Reputation: 61
Getting an error when trying to sort decoded json:
Exception has occurred. _TypeError (type '(dynamic, dynamic) => dynamic' is not a subtype of >type '(dynamic, dynamic) => int' of 'compare')"
The json looks like this: https://i.sstatic.net/Run1P.jpg
I guess sort(), which has a compareTo in the body, returns int (-1, 1) and that is the 'dynamic, dynamic) => int' part, and the decoded json is a Map of dynamic by default 'dynamic, dynamic) => dynamic', so I need to cast something. But I can't see what function in my code is returning a dynamic? Or even if that is the issue?
if (localData != null && localData["articles"] != null) {
the error occurs on the next line:
localData["articles"].toList().sort((a, b) =>
a["publishedAt"] != null && b["publishedAt"] != null
? b["publishedAt"].compareTo(a["publishedAt"])
: null);
}
Upvotes: 0
Views: 207
Reputation: 3090
I have created one example for you where there are Categories list and added publishedAt field to sort it
Here is the json:
{
"Categories": [
{
"id": 1,
"name": "Restruants",
"publishedAt": "2019-05-07T00:36:38Z",
},
{
"id": 2,
"name": "Car Rental",
"publishedAt": "2019-06-07T00:36:38Z",
},
{
"id": 3,
"name": "Furniture",
"publishedAt": "2019-12-21T00:36:38Z",
},
{
"id": 4,
"name": "cars",
"publishedAt": "2019-08-10T00:36:38Z",
},
{
"id": 5,
"name": "Maintenance",
"publishedAt": "2019-03-15T00:36:38Z",
},
{
"id": 6,
"name": "Education",
"publishedAt": "2019-09-17T00:36:38Z",
},
{
"id": 7,
"name": "Finess",
"publishedAt": "2019-01-28T00:36:38Z",
},
{
"id": 8,
"name": "Electronics",
"publishedAt": "2019-09-19T00:36:38Z",
},
{
"id": 9,
"name": "Medical",
"publishedAt": "2019-12-25T00:36:38Z",
},
{
"id": 10,
"name": "Entirtainment",
"publishedAt": "2019-06-14T00:36:38Z",
}
]
}
Here is the model class where you can find sort method when we get data from json.
You can see the method start with comment below "// TO SORT ARRAY WITH publishedAt"
class ItemModel {
List<Category> categories;
ItemModel({
this.categories,
});
factory ItemModel.fromJson(Map<String, dynamic> json){
var accJson = json["Categories"] as List;
List<Category> accList = accJson.map((i) => Category.fromJson(i)).toList();
// TO SORT ARRAY WITH publishedAt
accList.sort((a, b) {
return a.publishedAt.compareTo(b.publishedAt);
});
return ItemModel(
categories: accList
);
}
Map<String, dynamic> toJson() => {
"Categories": new List<dynamic>.from(categories.map((x) => x.toJson())),
};
}
class Category {
int id;
String name;
String publishedAt;
IconPath iconPath;
Category({
this.id,
this.name,
this.publishedAt,
this.iconPath,
});
factory Category.fromJson(Map<String, dynamic> json) => new Category(
id: json["id"],
name: json["name"],
publishedAt: json["publishedAt"],
iconPath: iconPathValues.map[json["iconPath"]],
);
Map<String, dynamic> toJson() => {
"id": id,
"name": name,
"publishedAt": publishedAt,
"iconPath": iconPathValues.reverse[iconPath],
};
}
Below is the Future class to get list from static json file from assets and it will give you sorted result because we are already sort it in the "fromJson" method :
Future<ItemModel> fetchMovieList(BuildContext context) async {
final jsonCategory = await DefaultAssetBundle
.of(context)
.loadString('assets/CategoryList.json');
Map<String, dynamic> values = Map<String, dynamic>.from(jsonDecode(jsonCategory));
final mapJsonCategory = Map<String, dynamic>.from(values);
print(mapJsonCategory);
return ItemModel.fromJson(mapJsonCategory);
}
Hope it helps :)
Upvotes: 1