【发布时间】:2021-06-09 08:17:41
【问题描述】:
你好,我正在开发一个带有颤振的应用程序,我对 Futurebuilder 有一个错误,我不明白 请帮忙
我试图根据 id 与未来的 builder 获取 title 和 desc
这是我的 json
{"title":"15%GANT","desc":"donne 15 % sur les gants"}
这是我的未来
Future<Data> fetchData(int id, String type) async {
var queryParameters = {
'type': type,
'id': id,
};
String url = 'tartapain.bzh';
var uri = Uri.https(url, '/api/scan/get.php', queryParameters);
final response = await http.get(uri);
if (response.statusCode == 200) {
if (response.body != null) {
return Data.fromJson(json.decode(response.body));
}
} else {
throw Exception('Failed to load the post');
}
}
这是我的数据类
class Data {
final String title;
final String desc;
Data({
this.title,
this.desc,
});
factory Data.fromJson(Map<String, dynamic> json) {
print(json['desc'] + " " + json['title'] + "\n");
return Data(
desc: json['desc'],
title: json['title'],
);
}
}
这是 mt 初始化
Future<Data> futureData;
@override
void initState() {
super.initState();
futureData = fetchData(widget.id, 'get_info_id');
}
这是我的futureBuilder
FutureBuilder(
future: futureData,
initialData: [],
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(child: Text("Loading..."));
}
if (snapshot.hasData) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
snapshot.data.title,
style: TextStyle(color: Colors.white, fontSize: 18),
),
Text(
snapshot.data.desc,
style: TextStyle(color: Colors.white, fontSize: 12),
)
],
);
} else if (snapshot.hasError) {
return Text(
'${snapshot.error}',
style: TextStyle(color: Colors.white, fontSize: 12),
);
} else {
return CircularProgressIndicator(
valueColor:
new AlwaysStoppedAnimation<Color>(Colors.black));
}
})
我得到了这个错误:
type 'int' 不是 type 'Iterable' 的子类型
【问题讨论】:
-
在您的 fetchdata 函数中,您是否尝试将 id 更改为字符串而不是 int?
-
我将 id 更改为查询参数中的字符串,感谢它的工作
标签: json flutter http flutter-futurebuilder