Cretezy / redux_persist

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

Is it possible to force-clear serialized state if loading fails? #16

Closed jagged3dge closed 6 years ago

jagged3dge commented 6 years ago

Hi! Thank you for this fantastic package! Unfortunately, I get thrown SerializationException: Load: FormatException: Unexpected character on app restart. Seems there could be some issue when serializing/saving the state via the proxy wrapper (toJsonWrapper) method, as described in #10

I could attempt to investigate and fix that, if I could only clear the currently stored data from the storage. Is there a way I could force-clear the saved data via the FlutterStorage instance, while developing?

This is my AppState:

import 'package:gforgamers_flutter/fixtures/polls.dart';
import 'package:gforgamers_flutter/fixtures/user.dart';
import 'package:gforgamers_flutter/pages/auth/state/state.dart';
import 'package:gforgamers_flutter/pages/voting/state/state.dart';
import 'package:json_annotation/json_annotation.dart';

part 'app.state.g.dart';

/// The global state of the entire application
///
/// Each field is handled by its own Reducer.
/// Each segment of the application should have its own state slice,
/// referred here as a [field].
@JsonSerializable()
class AppState extends Object with _$AppStateSerializerMixin {
  // TODO: Remove fixture initialisations:
  // AuthState: remove `user: user`
  // VotingState: remove `poll: poll1`
  AppState({
    AuthState auth,
    VotingState voting,
  })  : this.auth = auth ?? new AuthState(user: user),
        this.voting = voting ?? new VotingState(poll: poll1);

  /// Authentication/authorization state
  AuthState auth;

  // Voting state
  VotingState voting;

  factory AppState.fromJson(Map<String, dynamic> json) =>
      _$AppStateFromJson(json);

  static AppState fromJsonWrapper(dynamic json) => AppState.fromJson(json);

}

I'm declaring the persistor instance like so:

final appPersistorStorage = new FlutterStorage('GforGamers');

final appPersistor = new Persistor<AppState>(
  storage: appPersistorStorage, // Or use other engines
  decoder: AppState.fromJsonWrapper,
);

Relevant error log in VS Code debug console:

E/flutter ( 5141): [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
E/flutter ( 5141): SerializationException: Load: FormatException: Unexpected character (at character 1138)
E/flutter ( 5141): ...:false},"endsAt":"2018-06-29T17:32:22.417130"},"busy":true,"error":null}}}}
E/flutter ( 5141):                                                                              ^
E/flutter ( 5141):
E/flutter ( 5141): #0      Persistor.load (package:redux_persist/redux_persist.dart:117:7)
E/flutter ( 5141): <asynchronous suspension>
E/flutter ( 5141): #1      new GforGamersApp (file:///D:/Work/GforGamers_Comparison_App/gforgamers_flutter/lib/app.dart:25:17)
E/flutter ( 5141): #2      main (file:///D:/Work/GforGamers_Comparison_App/gforgamers_flutter/lib/main.debug.local.dart:23:16)
E/flutter ( 5141): #3      _startIsolate.<anonymous closure> (dart:isolate/runtime/libisolate_patch.dart:279:19)
E/flutter ( 5141): #4      _RawReceivePortImpl._handleMessage dart:isolate/runtime/libisolate_patch.dart:165:12)
Cretezy commented 6 years ago

Wrap the first persistor.load in a try catch?

jagged3dge commented 6 years ago

I did some digging and found that this is somehow being caused by an extra ending brace } at the end of the JSON string of the serialized state. Since this is probably an issue with the serialization library I'm using, I think we can safely close this issue.

Thank you for the interest :+1:

PiercarloSlavazza commented 6 years ago

I did some digging and found that this is somehow being caused by an extra ending brace } at the end of the JSON string of the serialized state. Since this is probably an issue with the serialization library I'm using, I think we can safely close this issue.

@jagged3dge I am having the very same issue: have you been able to pinpoint the bug? Which serialization library are you using?

jagged3dge commented 6 years ago

@nullp01nt3r I'm using json_serializable package for serialization.

I couldn't, however, pinpoint which part of the state is causing the extra brace after being serialized. So, I had to resort to monkey-patching a personal fork of the json_serializable to handle the case, limited to this project.

I haven't been able to reproduce this issue on another project I've been working on, so, it's something quite relative to the object structure, it seems.

PiercarloSlavazza commented 6 years ago

it's something quite relative to the object structure, it seems.

@jagged3dge as a second tought, I do not believe it's serialization fault: if you want, have a look at: