【发布时间】:2020-01-18 12:45:09
【问题描述】:
我在 2 小时后就遇到了问题。 我有这个错误,我看到了更多的主题,但我无法解决它。
消息:_TypeError(类型'List'不是类型'Map'的子类型)颤动
我的模特:
class Theme {
int id;
String name;
Theme({this.id, this.name});
factory Theme.fromJson(Map<String, dynamic> json) {
return Theme(
id: json['id'],
name: json['name'],
);
}
Future<Theme> getThemes() async {
String url = 'http://10.0.2.2:3000/v1/api/theme';
final response =
await http.get(url, headers: {"Accept": "application/json"});
if (response.statusCode == 200) {
return Theme.fromJson(json.decode(response.body));
} else {
throw Exception('Failed to load themes');
}
}
}
我的主题屏幕:
class _Theme extends State<Theme> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Blackbox"),
),
body: Center(
child: FutureBuilder<t.Theme>(
future: t.Theme().getThemes(), //sets the getQuote method as the expected Future
builder: (context, snapshot) {
if (snapshot.hasData) {
new ListView.builder(
itemCount: _lengthList,
itemBuilder: (BuildContext context, int index) {
return Container(
child: new Text('${snapshot.data.name}'),
);
},
);//checks if the response returns valid data
} else if (snapshot.hasError) { //checks if the response throws an error
return Text("${snapshot.error}");
}
return CircularProgressIndicator();
},
),
),
);
}
}
我尝试了更多教程和不同的主题,但我遇到了同样的错误......
谢谢!
【问题讨论】:
-
你能发布你的json响应吗
标签: android flutter flutter-layout