Cretezy / redux_persist

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

State is never saved #63

Closed flipch closed 4 years ago

flipch commented 4 years ago

Hey there.

I must be doing something wrong but I just tried the example code and couldn't get my app state to ever persist. I can see it trying to load but json is never not null, nor any of my breakpoints on the toJson methods get hit.

  redux_persist: ^0.8.4
  redux_persist_flutter: ^0.8.2

main.dart

// Create Persistor
final persistor = Persistor<AppState>(
  storage: FlutterStorage(),
  serializer: JsonSerializer<AppState>(AppState.fromJson),
  debug: true,
);

// Load initial state
final initialState = await persistor.load();

And then on my main AppState class where my methods are declared. (they actually are being created through json_annotation)

@JsonSerializable(nullable: true)
@immutable
class AppState {
  final AuthState authState;

  factory AppState.initial() {
    return AppState(
      authState: AuthState.initial(),
    );
  }

  AppState({
    @required this.authState,
  });

  AppState copyWith({
    AuthState authState,
  }) {
    return AppState(
      authState: authState ?? this.authState,
    );
  }

  // Redirect Json Serialization to Generated methods.
  static AppState fromJson(dynamic json) =>
      json == null ? null : _$AppStateFromJson(json);
  dynamic toJson() => _$AppStateToJson(this);
}

I can see the fromJson getting called but I don't ever see the toJson, even after multiple different actions being dispatched.

Proof that the Actions are being dispatched

I/flutter (15674): [INFO] LoggingMiddleware: {Action: Instance of 'LoginAction', State: Instance of 'AppState', ts: 2020-06-24 12:14:49.867690}
I/flutter (15674): [INFO] LoggingMiddleware: {Action: Instance of 'LoginSuccessAction', State: Instance of 'AppState', ts: 2020-06-24 12:14:51.576101}

Could this possibly be an permissions setting within my android emulator?

flipch commented 4 years ago

Oh my lord. I forgot to add the persistor middleware.

middleware: [
  persistor.createMiddleware(),
  LoggingMiddleware.printer(),
],