Cretezy / redux_persist

Persist Redux State
https://pub.dartlang.org/packages/redux_persist
MIT License
130 stars 41 forks source link

Unhandled Exception: Converting object to an encodable object failed: Instance of 'AppState' #58

Closed Caldarie closed 4 years ago

Caldarie commented 4 years ago

Hi Charles,

I am still getting past multiple errors even after I uninstalled your packages:

  1. Unhandled Exception: SerializationException: On load: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String'
  2. Unhandled Exception: SerializationException: On load: type 'String' is not a subtype of type 'AppState'
    1. Unhandled Exception: SerializationException: On load: type 'String' is not a subtype of type 'List<Todo>' in type cast

Can you help me find the issue?

Edit: Couldn't fix it so I just went back to my master branch.

Cretezy commented 4 years ago

Hi, this seems like you are trying to load a string as a list. Can you please post your code?

Caldarie commented 4 years ago

Hi Charles,

Thank you for the previous message. I

got past the string error, but now i'm getting the following issue: Unhandled Exception: Converting object to an encodable object failed: Instance of 'AppState'

This is from Users

 static User fromJson(dynamic json) {
    return json != null 
    ? User(email: json["email"], 
          id: json["id"], 
          displayName: json["displayName"],
          photoUrl: json["photoUrl"])
    : User();
  }

  dynamic toJson() {
    return {
      "email": email,
      "id": id,
      "displayName": displayName,
      "photoUrl": photoUrl,
    };
  }

This is from Appstate

 static AppState fromJson(dynamic json) {
    log(json.toString());
    //handles null otherwise returns exception
    if(json != null){
        return AppState(users: User.fromJson(json['users']));
      }else{
        return AppState.initial();
      }
    }

  dynamic toJson() => {'users':users.toJson()};

This is from Main

void main() async{
  WidgetsFlutterBinding.ensureInitialized();
  UserRepository userRepository;

  final persistor = Persistor<AppState>(
    storage: FlutterStorage(),
    serializer: JsonSerializer<AppState>(AppState.fromJson),
    );
  log(persistor.toString());

  final initialState = await persistor.load();

  final store = Store<AppState>(
          appReducer, 
          initialState: initialState ?? AppState.initial(), 
          middleware: (createAuthenticationMiddleware(
                userRepository ?? FirebaseUserRepository(FirebaseAuth.instance)))
            ..addAll([persistor.createMiddleware()]),
        );

  runApp(App(store: store));
}
Cretezy commented 4 years ago

For AppState, instead of users.toJson(), you need to loop the array:

  dynamic toJson() => {'users':users.map((user) => user.toJson()).toList()};
Caldarie commented 4 years ago

Hi Charles,

Thanks again for the input.

While implementing your solution, I have come across an unusual error where persistor.load() continues to output the exact same values for the appstate, even though I have made changes to the appState. For example, the output below is exactly the same, despite logging in with different users. users: {email: SampleName@gmail.com, id: rCQmlm3XHQhVPT1Sg8HzRTE34Gb2, displayName: SampleName}`

To fix this error, I have tried to: a) restart the app b) logged in with a different user c) manually purge by replacing persistor.load() with a null d) run clean and build flutter apk e) restarting the ide

Any ideas on how to solve this issue?

Edit: Nevermind, logging out fixes that issue. Feel stupid haha