我想从我的API加载一些数据。
我为我想要接收的数据建立模型,如下所示:
int? id;
int? createdAt;
int? eventsId;
int? userId;
String? deleteindex;
int? pferdeId;
Vertreter? vVertreter;
VertreterModell(
{this.id,
this.createdAt,
this.eventsId,
this.userId,
this.deleteindex,
this.pferdeId,
this.vVertreter});
VertreterModell.fromJson(Map<String, dynamic> json) {
id = json['id'];
createdAt = json['created_at'];
eventsId = json['events_id'];
userId = json['user_id'];
deleteindex = json['deleteindex'];
pferdeId = json['pferde_id'];
vVertreter = json['_vertreter'] != null
? new Vertreter.fromJson(json['_vertreter'])
: null;
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['created_at'] = this.createdAt;
data['events_id'] = this.eventsId;
data['user_id'] = this.userId;
data['deleteindex'] = this.deleteindex;
data['pferde_id'] = this.pferdeId;
if (this.vVertreter != null) {
data['_vertreter'] = this.vVertreter!.toJson();
}
return data;
}
}
class Vertreter {
String? name;
Profilbild? profilbild;
Vertreter({this.name, this.profilbild});
Vertreter.fromJson(Map<String, dynamic> json) {
name = json['name'];
profilbild = json['profilbild'] != null
? new Profilbild.fromJson(json['profilbild'])
: null;
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['name'] = this.name;
if (this.profilbild != null) {
data['profilbild'] = this.profilbild!.toJson();
}
return data;
}
}
class Profilbild {
String? url;
Profilbild({this.url});
Profilbild.fromJson(Map<String, dynamic> json) {
url = json['url'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['url'] = this.url;
return data;
}
}
和我的API代码看起来像这样:
const storage = FlutterSecureStorage();
var token = await storage.read(key: "_authToken");
var url = Uri.parse('${Constants.BASE_URL}/vertretung/vertreter');
var headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Bearer $token',
};
var res = await http.get(url, headers: headers);
final result = json.decode(res.body);
print(result);
return result.map<VertreterModell>(VertreterModell.fromJson).toList();
}
每次我调用API,我得到一个错误:
类型"(Map<字符串,dynamic>) =比;vertretermodel '不是type的子类型'(dynamic) =>vertretermodel ' of 'f'
你知道我做错了什么吗?它适用于我制作的其他api函数。提前感谢!
result
的运行时类型为List<dynamic>
,因此不能使用带Map<String, dynamic>
参数的函数来映射。
由于result
的类型为dynamic
,但运行时类型为List<dynamic>
,您仍然可以使用map
,但没有关于它的使用的静态分析。
你可以试试
result.map<VertreterModell>((data) => VertreterModell.fromJson(data) ).toList();
由于data
在这里有dynamic
类型。
final oldList = jsonDecode('[{"a": "1"},{"b": "2"},{"c": "3"}]');
print(oldList.runtimeType); // List<dynamic>
//runtime error, but no static analysis error
oldList.map<MyClass>(MyClass.fromJson).toList();
//static analysis error
(oldList as List<dynamic>).map<MyClass>(MyClass.fromJson).toList();
//no error
final newList = oldList.map<MyClass>((data) => MyClass.fromJson(data)).toList();
print(newList);
尝试使用此工具创建模型。https://ashamp.github.io/jsonToDartModel/
并使用此方法调用api
const storage = FlutterSecureStorage();
var token = await storage.read(key: "_authToken");
var url = Uri.parse('${Constants.BASE_URL}/vertretung/vertreter');
var headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Bearer $token',
};
var res = await http.get(url, headers: headers);
final result = json.decode(res.body);
print(result);
YourModel yourModelName = YourModel.fromJson(result);
///return if you want data usign **yourModelName** object.
return yourModelName;
}
我希望这些东西能解决你的问题。