Flutter Snapshot.hasData 总是返回 true
Flutter Snapshot.hasData is always returning true
我需要从服务器获取数据,有时数据可能是空的 来自 logcat 的响应是数据 snapshot.data
正在返回我的模型 Instance of 'MyItems'
的实例以及来自服务器的响应数据不可用是
{
"data": []
}
问题是 snapshot.hasData
对于我测试过的空响应总是返回 true snapshot.data == null 仍然是 true。
型号class
import 'dart:convert';
MyItems myItemsFromJson(String str) => MyItems.fromJson(json.decode(str));
String myItemsToJson(MyItems data) => json.encode(data.toJson());
class MyItems {
MyItems({
this.dataa,
});
List<Datumm> dataa;
factory MyItems.fromJson(Map<String, dynamic> json) => MyItems(
dataa: List<Datumm>.from(json["data"].map((x) => Datumm.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"data": List<dynamic>.from(dataa.map((x) => x.toJson())),
};
}
class Datumm {
Datumm({
this.userId,
this.Name,
this.MiddelName,
});
String userId;
String Name;
String MiddelName;
factory Datumm.fromJson(Map<String, dynamic> json) => Datumm(
userId: json["user_id"],
Name: json["Name"],
MiddelName: json["MiddleName"],
);
Map<String, dynamic> toJson() => {
"user_id": userId,
"Name": crbtCode,
"MiddelName": artistName,
};
}
我的未来api来电
Future<MyItems> getUdata(String aName) async {
var url =
'https://cvbgng.com/test/${aName}';
final response = await http.get(url).timeout(Duration(seconds: 15000));
if (response.statusCode == 200) {
return MyItems.fromJson(json.decode(response.body));
} else {
throw Exception('Faild to load');
}
}
检查条件 snapshot.data.dataa.isEmpty
如果为真,则您的响应为空。
我需要从服务器获取数据,有时数据可能是空的 来自 logcat 的响应是数据 snapshot.data
正在返回我的模型 Instance of 'MyItems'
的实例以及来自服务器的响应数据不可用是
{
"data": []
}
问题是 snapshot.hasData
对于我测试过的空响应总是返回 true snapshot.data == null 仍然是 true。
型号class
import 'dart:convert';
MyItems myItemsFromJson(String str) => MyItems.fromJson(json.decode(str));
String myItemsToJson(MyItems data) => json.encode(data.toJson());
class MyItems {
MyItems({
this.dataa,
});
List<Datumm> dataa;
factory MyItems.fromJson(Map<String, dynamic> json) => MyItems(
dataa: List<Datumm>.from(json["data"].map((x) => Datumm.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"data": List<dynamic>.from(dataa.map((x) => x.toJson())),
};
}
class Datumm {
Datumm({
this.userId,
this.Name,
this.MiddelName,
});
String userId;
String Name;
String MiddelName;
factory Datumm.fromJson(Map<String, dynamic> json) => Datumm(
userId: json["user_id"],
Name: json["Name"],
MiddelName: json["MiddleName"],
);
Map<String, dynamic> toJson() => {
"user_id": userId,
"Name": crbtCode,
"MiddelName": artistName,
};
}
我的未来api来电
Future<MyItems> getUdata(String aName) async {
var url =
'https://cvbgng.com/test/${aName}';
final response = await http.get(url).timeout(Duration(seconds: 15000));
if (response.statusCode == 200) {
return MyItems.fromJson(json.decode(response.body));
} else {
throw Exception('Faild to load');
}
}
检查条件 snapshot.data.dataa.isEmpty
如果为真,则您的响应为空。