Hi i am building an app and trying to parse nested array of object from an api call back and getting this error
Type String is not the sub Type of Map<String, dynamic>
Here is the model class
class Tournament {
String id;
String title;
String roomID;
String roomPass;
String map;
String type;
String date;
String time;
int joined;
String createdBy;
List<UserIds> joinedUsers;
Tournament(
{this.createdBy,
this.joinedUsers,
this.id,
this.date,
this.map,
this.roomID,
this.roomPass,
this.time,
this.title,
this.type,
this.joined});
factory Tournament.fromJson(Map<String, dynamic> json) {
var list = json['joinedUsers'] as List;
List<UserIds> userList =
list.map((data) => UserIds.fromJson(data)).toList();
return Tournament(
id: json['_id'],
title: json['title'],
roomID: json['roomId'],
roomPass: json['roomPass'],
map: json['mapType'],
type: json['type'],
date: json['date'],
time: json['time'],
joined: json['joined'],
createdBy: json['createdBy'],
joinedUsers: userList);
}
}
class UserIds {
String userId;
UserIds({this.userId});
factory UserIds.fromJson(Map<String, dynamic> parsedJson) {
return UserIds(userId: parsedJson['\$oid']);
}
}
this is the json call back i got
{
"_id": {
"$oid": "5f1c47f2c3c051d9828b1697"
},
"joinedUsers": [{
"$oid": "5f18621d6fca9d3e70a9fabe"
}, {
"$oid": "5f1a7609f7f69d2a1064e5ec"
}],
"title": "HomeComing",
"date": "20-02-2020",
"time": "8:22 Am",
"roomId": "12345",
"roomPass": "12223",
"joined": {
"$numberInt": "9"
},
"mapType": "Erangle",
"type": "Dual",
"createdBy": {
"$oid": "5f16d9bde0fd621dec10e1c5"
},
"__v": {
"$numberInt": "0"
}
}
now when i run this it gave me an error that type String is not the sub type of Map<String, dynamic> i dont know what wrong i am doing right now
Could you try:
list.map((data) => UserIds.fromJson(JSON.parse(data)).toList();
Could be parse problems, you could log first your elements in your lambda by this to verify what it gives:
list.map((element) =>
{
console.log(data);
//UserIds.fromJson(JSON.parse(data)).toList();
});
Just check out this example that I have made for userIds: I have taken your json locally.
import 'dart:convert';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(home: HomePage());
}
}
class UserIds {
String userId;
UserIds({this.userId});
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
bool _isLoading = false;
List<UserIds> userIdsList = List();
@override
void initState() {
super.initState();
getData();
}
getData() async {
String data =
await DefaultAssetBundle.of(context).loadString("json/parse.json");
Map jsonData = json.decode(data);
jsonData['joinedUsers'].forEach((item) {
item.forEach((key, value) {
print('This is the Key $key value $value');
userIdsList.add(UserIds(userId: value));
});
});
print('This is the list ${userIdsList.length}');
}
@override
Widget build(BuildContext context) {
return Scaffold(body: Text(''));
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.