Coder Social home page Coder Social logo

After upgrade from 1.3.1 to 1.4.0 get_storage i get type 'LastCall' is not a subtype of type 'Map<String, dynamic>' error about get_storage HOT 10 OPEN

jonataslaw avatar jonataslaw commented on August 24, 2024
After upgrade from 1.3.1 to 1.4.0 get_storage i get type 'LastCall' is not a subtype of type 'Map' error

from get_storage.

Comments (10)

hhstore avatar hhstore commented on August 24, 2024 2

ref:

solution:

  • fix for dart null safety.
  • like this example:
  /// get many:
  Map<String, UserEntity> users() {
    var key = keys.users();

    /// (UserEntity, has fromJson, toJson)
    var raw = kv.read(key) ?? {};
    print('users:$raw, ${raw.runtimeType}');

    // if (raw is Map<String, UserEntity>) {
    //   return raw;
    // }

    // return raw;
    var result = <String, UserEntity>{};

    raw.forEach((key, value) {
      print('$key, $value, ${value.runtimeType}');
      result[key] = value is UserEntity ? value : UserEntity.fromJson(value); // TODO: here!!!
      // result[key] = (value as UserEntity?)!;
    });

    return result;
    // return raw ?? {};
    // return Map<String, UserEntity>.from(out ?? {});
  }

key points:

  • required: forEach + value is UserEntity
  raw.forEach((key, value) {}


  result[key] = value is UserEntity ? value : UserEntity.fromJson(value); // TODO: here!!!

from get_storage.

jpandco avatar jpandco commented on August 24, 2024 1

yes the same error. if you write Model then read he will return from memory and the return will keep the Type Model.
but if the model was not in memory he will get the model from storage and the return will be _InternalLinkedHashMap<String, dynamic>

from get_storage.

dreamer2q avatar dreamer2q commented on August 24, 2024 1

When your data is stored in memory, you can get the exact type as you want, otherwise you will get json decoded data from LocalStorage (usually typed Map or List depending your data type).

Therefore, you have to check the data type before you try parse fromJson.

This is the problem of flutter, you have to compromise.

from get_storage.

iammazharul avatar iammazharul commented on August 24, 2024 1

may be i am late, but i think it's still helpful. because me also faced the same problem. in my case i am forget to convert it in JSON format before wright action.In my case:

error: get_storage is not a subtype of type 'Map<String, dynamic>'
wrong way:
box.write('user', data.userData);

right way:

          UserData userData;
          GetStorage box = GetStorage();
          box.write('user', data.userData.toJson());

userData = UserData.fromJson(box.read('user'));

from get_storage.

aminkhan1 avatar aminkhan1 commented on August 24, 2024 1

i had this problem. i fix this problem like this
if (store.read('docs') is DocsModel) { return model = store.read('docs'); } else { return model = DocsModel.fromJson(store.read('docs')); }

from get_storage.

halkportal970 avatar halkportal970 commented on August 24, 2024

After I downgrade to 1.3.2 code works fine.

from get_storage.

ashbressler avatar ashbressler commented on August 24, 2024

I'm experiencing the same.

Some of my observations are that the first time after loading the app GetStorage().read returns a json array. However once I call GetStorage().write then .read returns the Model that I wrote, not the JSON representation of that Model.

from get_storage.

jpandco avatar jpandco commented on August 24, 2024

After 1.4.0 upgrade, I get
type 'LastCall' is not a subtype of type 'Map<String, dynamic>'
error.
I have a model called LastCall, and I store data as json .
First call of loadLastCallList() function works perfect. Bu second time, third time etc fails.
First time list variable is a [0]: Map
But the other times list variable is a [0]:LastCall
for that fromJson fails to convert list[i]

Why list variable is first time is a Map but the other times is a Model(LastCall)
List list = GetStorage().read("lastCallList");
I am excepting it all the time as a Map variable.

Future<void> loadLastCallList() async {
    var tempList = List<LastCall>();
      List<dynamic> list = GetStorage().read("lastCallList");
      if (list != null) {
        for (var i = 0; i < list.length; i++) {
          LastCall lastCall = LastCall.fromJson(list[i]);
          tempList.add(lastCall);
        }
      } else {
        lastCallList.clear();
      }
      lastCallList.assignAll(tempList);
}

i think this will fix your error

  Future<void> loadLastCallList() async {
    var tempList = List<LastCall>();
    var _data = GetStorage().read("lastCallList");
    
    var list = _data is List<LastCall> ? _data : Map<String, dynamic>.from(_data).entries.toList();
    
    if (list != null) {
      for (var i = 0; i < list.length; i++) {
        var _lastCall = list[i] is LastCall ? list[i] : LastCall.fromJson(list[i]);
        tempList.add(_lastCall);
      }
    } else {
      lastCallList.clear();
    }
    lastCallList.assignAll(tempList);
  }

from get_storage.

AlirezaDaryani avatar AlirezaDaryani commented on August 24, 2024

same problem, i build model data with fromJson and toJson, but 'read' dose not give me my model.

from get_storage.

hhstore avatar hhstore commented on August 24, 2024

may be i am late, but i think it's still helpful. because me also faced the same problem. in my case i am forget to convert it in JSON format before wright action.In my case:

error: get_storage is not a subtype of type 'Map<String, dynamic>'
wrong way:
box.write('user', data.userData);

right way:

          UserData userData;
          GetStorage box = GetStorage();
          box.write('user', data.userData.toJson());

userData = UserData.fromJson(box.read('user'));

Hive DB:

image

Protobuf:

MsgPack:

FlatBuffers:

My suggestion:

  • In fact, get storage can directly support protobuf or msgpack to define the data model. Wouldn't it be better?
  • The hive's way, a bit Inconvenient.
  • @jonataslaw

from get_storage.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.