Comments (10)
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.
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.
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.
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.
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.
After I downgrade to 1.3.2 code works fine.
from get_storage.
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.
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.
same problem, i build model data with fromJson and toJson, but 'read' dose not give me my model.
from get_storage.
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:
- isar/hive#126
- like HiveDB: https://github.com/hivedb/hive#store-objects
- use
hive_generator
automatically generateTypeAdapters
for almost any class. - register a
TypeAdapter
which converts the objectfrom and to
binary form. - https://docs.hivedb.dev/#/custom-objects/generate_adapter
Protobuf:
- The data definition for
hive
, very similar to the wayprotobuf
is defined. - https://github.com/dart-lang/protobuf
MsgPack:
- https://pub.dev/packages/msgpack_dart
- https://github.com/butlermatt/msgpack2
- https://github.com/ivoleitao/stash/tree/develop/packages/stash_hive
- https://pub.dev/packages/stash_hive
- https://msgpack.org/
FlatBuffers:
- Google FlatBuffers.
- https://github.com/google/flatbuffers
- dart lib: https://github.com/google/flatbuffers/blob/master/dart/README.md
My suggestion:
- In fact,
get storage
can directly supportprotobuf
ormsgpack
to define the data model. Wouldn't it be better? - The hive's way, a bit Inconvenient.
- @jonataslaw
from get_storage.
Related Issues (20)
- Documentation site
- GetStorage.init Error HOT 2
- await GetStorage.init(); Never returns after hot restart in Android Studio.
- GetSecureStorage
- GetStorage is not reliable for cross-thread operations. HOT 1
- When I install get_ Storage, run the program in a Windows environment, prompt me “ Run start ms-settings:developers” HOT 1
- PlatformException (PlatformException(channel-error, Unable to establish connection on channel., null, null)) HOT 5
- Get Storage not saving some data HOT 5
- Get Storage behaving abnormally
- when get_storage using in new android version 14 then application full lagged HOT 1
- Storage data persists even after delete AppData on Windows HOT 4
- Get storage not working offiline
- Error on Web Platform: localStorage.containsKey is not a function
- Is Flutter getstorage safe? HOT 2
- Running into problems on ios with get_storage
- After writing, run the App again to perform the read operation, and the previously saved content disappears. HOT 7
- Module 'path_provider_foundation' not found, Could not create task ':path_provider_android:generateDebugUnitTestConfig'.
- get_storage not saving data when key starts with underscore (_), private variable not saved by get_storage
- Getsorage not erasing data when using GetStroage().erase()
- update flutter to 3.22
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from get_storage.