'List<dynamic> json FormatException Invalid radix-10 number
I am trying to import json from a remote API and can not figure out what the proper map method is. I have tried numeros variations, but keep getting a formatexception error.
My json output is a list (member_jsonj) with a map:
[
{
"member_json":{
"nameTag":"@Gumdum",
"memberId":"031e78f2-e644-422d-8510-7a8f10cee1fb",
"memberName":"Gumdum",
"memberEmail":"[email protected]",
"likedDailyVerses":[78,79],
"likedDailyComments":[94],
}
}
]
I am trying to fill my member MemberModel by calling a fromJson
final MemberModel member = MemberModel.fromJson(result as List<dynamic>);
factory MemberModel.fromJson(List<dynamic> json) {
final memberId = json[0]['memberId'].toString();
final memberName = json[0]['memberName'].toString();
final memberEmail = json[0]['memberEmail'].toString();
final result = jsonDecode(json[0]['likedDailyComments'].toString());
final List<int> likedDailyComments = (result as List<int>).toList();
final nextResult = jsonDecode(json[0]['likedDailyVerses'].toString());
final List<int> likedDailyVerses = (result as List<int>).toList();
return MemberModel(
memberId: memberId,
memberName: memberName,
memberEmail: memberEmail,
likedDailyVerses: likedDailyVerses,
likedDailyComments: likedDailyComments );
Please, any help or pointers at this stage would be most appreciated!
Solution 1:[1]
Thank you to @julemand101 from the flutter discord channel for working with me on this problem and coming up with a solution.
import 'dart:convert';
const jsonInput = '''
[
{
"member_json":{
"nameTag":"@Gumdum",
"memberId":"031e78f2-e644-422d-8510-7a8f10cee1fb",
"memberName":"Gumdum",
"memberEmail":"[email protected]",
"likedDailyVerses":[78,79],
"likedDailyComments":[94]
}
}
]
''';
class MemberModel {
String nameTag;
String memberId;
String memberName;
String memberEmail;
List<int> likedDailyVerses;
List<int> likedDailyComments;
MemberModel({
required this.nameTag,
required this.memberId,
required this.memberName,
required this.memberEmail,
required this.likedDailyVerses,
required this.likedDailyComments,
});
factory MemberModel.fromJson(Map<String, dynamic> jsonMap) {
final json = jsonMap['member_json'] as Map<String, dynamic>;
return MemberModel(
nameTag: json['nameTag'] as String,
memberId: json['memberId'] as String,
memberName: json['memberName'] as String,
memberEmail: json['memberEmail'] as String,
likedDailyVerses: List.from(json['likedDailyVerses'] as List),
likedDailyComments: List.from(json['likedDailyComments'] as List),
);
}
}
void main() {
final jsonList = jsonDecode(jsonInput) as List<dynamic>;
final memberModels = [
for (final memberModelMap in jsonList)
MemberModel.fromJson(memberModelMap as Map<String, dynamic>)
];
print(memberModels.first.likedDailyVerses); // [78, 79]
}
due to my server output of json in json, I had to modify his solution a little with:
final json = jsonDecode(jsonMap['member_json'].toString());
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Gumdum |