Flutter 在颤振应用程序中处理JSON解析中的空值
我从数据库中获取详细信息,然后解析json值。下面是http请求的代码Flutter 在颤振应用程序中处理JSON解析中的空值,flutter,Flutter,我从数据库中获取详细信息,然后解析json值。下面是http请求的代码 Future <List> getData() async{ if(endofrecord == false){ try{ var body = { "uid" : dtguid, "deviceid": deviceid, "offset": offset}; var url = 'ht
Future <List> getData() async{
if(endofrecord == false){
try{
var body = { "uid" : dtguid, "deviceid": deviceid, "offset": offset};
var url = 'http://192.168.1.100:8080/get_recommended.php';
// Starting Web API Call.
var response = await http.post(url, body: json.encode(body)).timeout(Duration(seconds: 5),
onTimeout: (){
// throw Exception();
_showSnackBar(context,'Some issue with connectivity. Can not reached to server.',Colors.redAccent);
//or you can also
return null;
});
if(response.statusCode == 200){
final data = parsedataFromJson(response.body);
setState(() {
recommended = true;
_inProcess = false;
if(data.count == null){
count = 0;
}else{
offset = offset + 5;
print(offset);
count = data.count;
}
if(data.content.length > 0 && data.content[0].name != 'Empty'){
for (var i in data.content) {
lists.add(i);
}
}else{
nodata = 'No Record Found';
endofrecord = true;
_showSnackBar(context,nodata,Colors.redAccent);
}
});
print(lists.length);
}
}catch(e){
print("Exception Caught: $e");
_showSnackBar(context,'Some issue with connectivity. Could not connect to server.',Colors.redAccent);
}
return lists;
}else{
return null;
}
}
当http请求不返回数据时,我如何处理这种情况?您是否在
try{}catch{}中发现任何错误
block。如果没有遇到错误,请检查自定义JSON转换器。尝试在没有自定义JSON解析器的情况下进行测试,并使用将JSON转换为映射的普通转换器。如果仍然无法工作,请确保已导入
像这样的dart:async模块导入dart:asyncdart。如果它没有更改任何内容,请尝试使用
.then()
和.catch()
语法。如果不尝试检查后端数据库,它们可能出了问题。是否在try{}catch}中发现任何错误
block。如果没有遇到错误,请检查自定义JSON转换器。尝试在没有自定义JSON解析器的情况下进行测试,并使用将JSON转换为映射的普通转换器。如果仍然无法工作,请确保已导入
像这样的dart:async模块导入dart:asyncdart。如果它没有改变任何东西,请尝试使用
.then()
和.catch()
语法。如果不尝试检查您的后端数据库,将JSON转换为PODO可能有问题,您必须使用
一旦生成了模型,您就可以很容易地检查来自后端的空元素。要将JSON转换为PODO,您必须使用
一旦生成了模型,您就可以轻松地检查来自后端的空元素。尝试找出错误的确切行号。然后,在那里放置一个断点。然后,找出出错时
json
的值是多少(我认为这将是导致问题的变量,但可能是另一个变量)。这可能足以解决您的问题,否则请编辑问题。谢谢您的评论。我认为这个错误来自List.from(json[“pricing”].map((x)=>pricing.fromJson(x)),这一行。还有,在json解析中,检查工厂中的json==null
。您只需检查一下json==null
,然后基于此设置值。有可能这确实解决了您的问题,因为您指示的行没有检查空值。请尝试找出错误的确切行号。然后,在那里放置一个断点。然后,找出出错时json
的值是多少(我认为这将是导致问题的变量,但可能是另一个变量)。这可能足以解决您的问题,否则请编辑问题。谢谢您的评论。我认为这个错误来自List.from(json[“pricing”].map((x)=>pricing.fromJson(x)),这一行。还有,在json解析中,检查工厂中的json==null
。您只需检查一下json==null
,然后基于此设置值。有可能这实际上解决了您的问题,因为您指示的行没有检查空值。如何在dart文件中访问此项。因为它需要3个参数。如何在dart文件中访问此参数。因为它需要3个参数。
import 'dart:convert';
DatabyPrice databyPriceFromJson(String str) => DatabyPrice.fromJson(json.decode(str));
class DatabyPrice {
DatabyPrice({
this.count,
this.content,
this.success,
});
int count;
List<Content> content;
bool success;
factory DatabyPrice.fromJson(Map<String, dynamic> json) => DatabyPrice(
count: json["count"],
content: List<Content>.from(json["content"].map((x) => Content.fromJson(x))),
success: json["success"],
);
}
class Content {
Content({
this.name,
this.uid,
this.pic,
this.state,
this.country,
this.lastLogin,
this.tabout,
this.averageOrating,
this.pricing,
});
String name;
int uid;
String pic;
String state;
String country;
String tabout;
String lastLogin;
String averageOrating;
List<Pricing> pricing;
factory Content.fromJson(Map<String, dynamic> json) => Content(
name: json == null ? 'Empty' : json["name"],
uid: json == null ? 0 :json["uid"],
pic: json == null ? 'Empty' :json["pic"],
state: json == null ? 'Empty' :json["state"],
tabout: json == null ? 'Empty' :json["tabout"],
country: json == null ? 'Empty' :json["country"],
lastLogin: json == null ? 'Empty' : json["last_login"],
averageOrating: json == null ? '0' :json["average_orating"],
pricing: List<Pricing>.from(json["pricing"].map((x) => Pricing.fromJson(x))),
);
}
class Pricing {
Pricing({
this.uid,
this.price,
this.serviceType,
});
int uid;
int price;
String serviceType;
factory Pricing.fromJson(Map<String, dynamic> json) => Pricing(
uid: json == null ? 0 :json["uid"],
price: json == null ? 0 :json["price"],
serviceType: json == null ? 'Empty' :json["service_type"],
);
}
I/flutter ( 5255): Receiver: null
I/flutter ( 5255): Tried calling: []("pricing")
I/flutter ( 5255): Exception Caught: NoSuchMethodError: The method '[]' was called on null.