Cretezy / redux_persist

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

Persistor debug: Error while loading: SerializationException: On load: NoSuchMethodError: The getter 'length' was called on null. #28

Closed raymondurgelles closed 5 years ago

raymondurgelles commented 5 years ago

Good Day!!! Thanks for a very nice plugin. I am new to mobile dev and i just recently learned about redux and persist. When I was using this plugin version 0.7.0, my code is working, when i upgraded, and followed what was written in readme, i get this error

Persistor debug: Starting load... I/flutter ( 8521): Persistor debug: Loading from storage I/flutter ( 8521): Persistor debug: Running load raw transformations I/flutter ( 8521): Persistor debug: Deserializing I/flutter ( 8521): Persistor debug: Error while loading: SerializationException: On load: NoSuchMethodError: The getter 'length' was called on null. I/flutter ( 8521): Receiver: null I/flutter ( 8521): Tried calling: length E/flutter ( 8521): [ERROR:flutter/shell/common/shell.cc(188)] Dart Error: Unhandled exception: E/flutter ( 8521): SerializationException: On load: NoSuchMethodError: The getter 'length' was called on null. E/flutter ( 8521): Receiver: null E/flutter ( 8521): Tried calling: length E/flutter ( 8521): #0 Persistor.load (package:redux_persist/src/persistor.dart:131:7) E/flutter ( 8521): E/flutter ( 8521): #1 createStore (package:halal_traveller/store/store.dart:21:38) E/flutter ( 8521): E/flutter ( 8521): #2 main (package:my_app/main.dart:15:23) E/flutter ( 8521): E/flutter ( 8521): #3 _startIsolate. (dart:isolate/runtime/libisolate_patch.dart:289:19) E/flutter ( 8521): #4 _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:171:12)

my code is like this. I have a state with a bool constructor isInit which tells me if the app already opened once, so

`class AppState { /// final bool isInit; /// AppState ({ /// this.isInit = false /// }); /// static AppState fromJson (dynamic map) { return AppState ( /// isInit: map["isInit"] as bool, /// ) }

Map toJson() { return { /// 'isInit': isInit, /// } } /// }`

then I have this createStore(),

`Future<Store> createStore() async { final persistor = Persistor( storage: FlutterStorage(), serializer: JsonSerializer(AppState.fromJson), debug: true, );

final middleware = [persistor.createMiddleware()];

middleware.add(new LoggingMiddleware.printer());

var initialState = await persistor.load();

return new Store( appReducer, middleware: middleware, initialState: initialState ?? AppState() ); }`

then on main() i have store = await createStore(); before runApp;

when I run the code, the error above is the result.

when i read persistor.dart, i see that the error comes from here, base from debug:

try { state = serializer.decode(data); } catch (error) { throw SerializationException('On load: ${error.toString()}'); }

i think the reason is data is null on first call since there is still no file existing.

So when I added if (data != null) before state = serializer.decode(data), the error is gone.

by the way i am using flutter.

Cretezy commented 5 years ago

Good point, there's a missing null check in the JsonSerializer, will add!

Cretezy commented 5 years ago

Pushed 0.8.0-rc.1 of redux_persist with a fix for this! Let me know if that's good.

raymondurgelles commented 5 years ago

It works perfectly! thanks! thumbs up!!!!

diogo-ribeiro-bitmaker commented 5 years ago

@Cretezy Still happens to me since the data in 'state = serializer.decode(data)' is null and it sends the null to the AppState.fromJson which gets an the same exception. I'm using the 0.8.0-rc.1 of redux_persist. Please can you help?

Cretezy commented 5 years ago

The AppState.fromJson must be able to handle a null state. It's by design for now, because you might want to return an empty state

diogo-ribeiro-bitmaker commented 5 years ago

Oh ok.. Ill check it out tomorrow and I'll let you know if I need anything. Thanks for the fast response! Keep up the good work! :)

Cretezy commented 5 years ago

Thank you, keep giving feedback, it's always helpful to have someone else's opinion! :)