Open Diy2210 opened 3 years ago
Hey! The only thing I see might be the issue is with the method saveCategories
.
You're using box.put('categories', category)
, the put
method overrides the key 'categories', to add multiple entries to the box you should either use different keys (i.e. box.put(category.id, category)
) or use the box.add(category)
method that gives an auto incremented key to each entry.
Edit: and in the cacheCategories
method since you are passing it a list, you should iterate through it and add each list entry to the box.
Pro tip: avoid using the dynamic
type when possible, it creates too much errors.
Do something like
Future<void> cacheCategories(List<Map> categoriesData) {
categoriesData.forEach((Map category) {
var categories = Categories(
id: category['id'] as int,
parentID: category['parent_id'] as int,
title: category['title'] as String,
price: category['price'] as int,
badgeCount: category['badges_count'] as int,
);
saveCategories(categories);
});
}
And in the jsonDecode part:
var jsonResponse = (convert.jsonDecode(resp.body) as Map)?.cast<String, dynamic>();
var categories = (jsonResponse['data'] as List).cast<Map>();
CategoriesDataHelper().cacheCategories(categories);
You might have to tweak a bit the code to work, but working with json is kind of complicated because of the type casts in dart.
Hi, I'm new in dart. I want save decode json in box => JSON like this:
I create model and generate adapter, init hive.box. My model:
And class helper for save it:
This method for save: