initJsonDecoders function

void initJsonDecoders(
  1. Map<Type, ModelDecoderFunction> value
)

Initializes JSON decoders for custom types (if you ever need to store anything custom in shared preferences), so they can be easily restored from SharedPreferences. Call this method someplace at the beginning of your app, just before you initialize LiteState controllers so that controllers can have access to this data before they are initialized themselves. Decoder MUST be a STATIC function that creates instances of custom classes from a map e.g. static AuthData decode(Map map) { return AuthData( type: map'type', token: map'token', userName: map'userName', ); } this function converts a Map, stored in SharedPreferences into a user defined object. In this case a custom class called AuthData

IMPORTANT! Before decoding anything, you need to encode it first but to be able to be encoded to JSON your custom classes must implement LSJsonEncodable interface from LiteState package. See AuthData in an example project it simply makes sure that your class contains "encode()" method that will convert your instance to a Map

Implementation

void initJsonDecoders(Map<Type, ModelDecoderFunction> value) {
  for (var v in value.entries) {
    final key = v.key.toString();
    if (key.contains('<')) {
      throw 'Encodable type must not be generic. Actual type: $key';
    }
    if (!_jsonDecoders.containsKey(key)) {
      _jsonDecoders[key] = v.value;
    }
  }
}