Closed DiegoFHG closed 4 years ago
I tried using this library for persisting. However, state is not persisting, and errors show but are not so descriptive.
My main file:
final persistor = Persistor<AppState>( storage: FlutterStorage(), serializer: JsonSerializer<AppState>(AppState.fromJson), debug: true ); AppState initialState; Store<AppState> store; void main() async { WidgetsFlutterBinding.ensureInitialized(); initialState = await persistor.load(); store = Store<AppState>( reducer, initialState: initialState, middleware: [persistor.createMiddleware()] ); final HttpLink httpLink = HttpLink( uri: 'http://192.168.1.200:3000/dev/graphql' ); final AuthLink authLink = AuthLink( getToken: () => 'Bearer ${store.state.authToken}' ); final Link link = authLink.concat(httpLink); getIt.registerSingletonAsync<ApiClient>(() async { final apiClient = ApiClient( client: GraphQLClient( cache: InMemoryCache(), link: link, ), ); return apiClient; }); BlocSupervisor.delegate = SimpleBlocDelegate(); runApp(App(store: store)); }
AppState:
class AppState { List<Item> cart; String authToken; CognitoUser user; AppState({ this.cart, this.authToken, this.user }); AppState.fromAppState(AppState another) { cart = another.cart; authToken = another.authToken; user = another.user; } double get cartTotal { double total = 0.00; if (this.cart == null) return 0.00; for (Item item in this.cart) { total += item.price; } return total; } static AppState fromJson(appState) { if (appState != null) return AppState( authToken: appState['authToken'], user: CognitoUser( appState['user']['username'], CognitoUserPool( 'XX-XX-XXXXXXX', 'XXXXXXXXXXXXXXX' ) ) ); return AppState(); } dynamic toJson(dynamic json) { return { "authToken": authToken, "user": { "username": user.username } }; } }
Logs:
flutter: Persistor debug: Starting save... flutter: Persistor debug: Running save transformations flutter: Persistor debug: Serializing flutter: Persistor debug: Error while saving: Converting object to an encodable object failed: Instance of 'AppState' [VERBOSE-2:ui_dart_state.cc(157)] Unhandled Exception: Converting object to an encodable object failed: Instance of 'AppState' #0 _JsonStringifier.writeObject (dart:convert/json.dart:659:7) #1 _JsonStringStringifier.printOn (dart:convert/json.dart:846:17) #2 _JsonStringStringifier.stringify (dart:convert/json.dart:831:5) #3 JsonEncoder.convert (dart:convert/json.dart:260:30) #4 JsonCodec.encode (dart:convert/json.dart:168:45) #5 JsonSerializer.encode (package:redux_persist/src/serialization.dart:28:35) #6 Persistor.save (package:redux_persist/src/persistor.dart:155:29) #7 Persistor.createMiddleware.<anonymous closure>.<anonymous closure>(package:redux_persist/src/persistor.dart:67:56) #8 _rootRun (dart:async/zone.dart:1180:38) #9 _CustomZone.run (dart:async/zone.dart:1077:19) #10 _CustomZone.runGuarded (dart:async/zone.dart:979:7) #11 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1019:23) #12 _rootRun (dart:asy<…>
I'm testing on iOS.
So I was not making the right mutation on login, so now is fixed. And the above error was due to trying to persist empty state.
I tried using this library for persisting. However, state is not persisting, and errors show but are not so descriptive.
My main file:
AppState:
Logs:
I'm testing on iOS.