flutter_localstorage icon indicating copy to clipboard operation
flutter_localstorage copied to clipboard

Migration Error: Types in Lists are Preserved in Latest Version

Open moneer-muntazah opened this issue 4 years ago • 1 comments

Hi

I am migrating my Flutter application to use package version 3.0.6+9 instead of 2.0.0 when calling setItem('key', <MyModel>[instance]), In the old version getItem gets me a list of _InternalLinkedHashMap<String, dynamic>, but in the new version the type is preserved, and I get a list of MyModel

For example if my model is such

class MyModel {
  int id;

  MyModel(this.id);

  Map<String, dynamic> toJson() => {'id': id};
}

And somewhere in my application I have Text(e['id'].toString()); this will return a runtime error with after upgrading.

~~I have a question. How are you preserving the type. I looked at setItem and it looks like it simply calls toJson and pass that Map<String, dynamic> along. Then how come I get MyModel instances when I fetch the data back with getItem? Can you point out the part of the code that's doing that?~~

I think this might be happening due to calling toJson manually vs. calling json.encode in the older version

Could you please update the documentation to point out this migration issue.

Thank you

Cheers

moneer-muntazah avatar Feb 02 '21 07:02 moneer-muntazah

It seems as of the third version, if getItem is called, and passed a list while a Flutter application is running (meaning the same instance it was running in when setItem was called), it returns data with the same data type it was put in using setItem , but if called after the app is terminated, it returns an _InternalLinkedHashMap.

moneer-muntazah avatar Mar 02 '21 09:03 moneer-muntazah

Please upgrade to the latest version. The API is now closer to the web, and always returns String, leaving deserialization up to you to implement.

lesnitsky avatar Apr 01 '24 18:04 lesnitsky