futurebuilder 和 http json 的颤动错误
flutter error with futurebuilder and http json
你好,我正在开发一个带有 flutter 的应用程序,我对 Futurebuilder 有一个错误,我不明白
请帮忙
我正在尝试根据 id 与未来的构建者一起获取标题和描述
这是我的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
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 init
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' is not a subtype of type 'Iterable'
好像是ID应该是String类型导致的错误。
这解决了问题。
我建议在您的模型中实施一些检查以防值为空。
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']!=null)?json['desc']:'no desc',
title: (json['title']!=null)?json['title']:'no title',
);
}
}
你好,我正在开发一个带有 flutter 的应用程序,我对 Futurebuilder 有一个错误,我不明白 请帮忙
我正在尝试根据 id 与未来的构建者一起获取标题和描述
这是我的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
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 init
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' is not a subtype of type 'Iterable'
好像是ID应该是String类型导致的错误。 这解决了问题。
我建议在您的模型中实施一些检查以防值为空。
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']!=null)?json['desc']:'no desc',
title: (json['title']!=null)?json['title']:'no title',
);
}
}