[英]Flutter problem with parsing Complex json data
I want to make a get query with some json data I wrote the class using this web site https://app.quicktype.io/ and it gave me this code
import 'dart:convert';
class Jsonclass {
final Imei imei;
Jsonclass({
this.imei,
});
factory Jsonclass.fromJson(Map<String, dynamic> json) => Jsonclass(
imei: Imei.fromJson(json["imei"]),
);
Map<String, dynamic> toJson() => {
"imei": imei.toJson(),
};
}
class Imei {
final String name;
Test test;
Imei({
this.name,
this.test,
});
factory Imei.fromJson(Map<String, dynamic> json) => Imei(
name: json["name"],
test: Test.fromJson(json["test"]),
);
Map<String, dynamic> toJson() => {
"name": name,
"test": test.toJson(),
};
}
class Test {
String batterieState;
String wifiState;
String pixelState;
String bluetoothState;
String gpsState;
String tactileState;
String bafleState;
String microState;
String vibreurState;
String camerafrontState;
String camerabackState;
String boutonState;
String usbState;
String kitState;
String geroscopeState;
String empreinteState;
String captState;
String greyState;
String proximiteState;
String lumiereState;
Test({
this.batterieState,
this.wifiState,
this.pixelState,
this.bluetoothState,
this.gpsState,
this.tactileState,
this.bafleState,
this.microState,
this.vibreurState,
this.camerafrontState,
this.camerabackState,
this.boutonState,
this.usbState,
this.kitState,
this.geroscopeState,
this.empreinteState,
this.captState,
this.greyState,
this.proximiteState,
this.lumiereState,
});
factory Test.fromJson(Map<String, dynamic> json) => Test(
batterieState: json["batterieState"],
wifiState: json["wifiState"],
pixelState: json["pixelState"],
bluetoothState: json["bluetoothState"],
gpsState: json["gpsState"],
tactileState: json["tactileState"],
bafleState: json["bafleState"],
microState: json["microState"],
vibreurState: json["vibreurState"],
camerafrontState: json["camerafrontState"],
camerabackState: json["camerabackState"],
boutonState: json["boutonState"],
usbState: json["usbState"],
kitState: json["kitState"],
geroscopeState: json["geroscopeState"],
empreinteState: json["empreinteState"],
captState: json["captState"],
greyState: json["greyState"],
proximiteState: json["proximiteState"],
lumiereState: json["lumiereState"],
);
Map<String, dynamic> toJson() => {
"batterieState": batterieState,
"wifiState": wifiState,
"pixelState": pixelState,
"bluetoothState": bluetoothState,
"gpsState": gpsState,
"tactileState": tactileState,
"bafleState": bafleState,
"microState": microState,
"vibreurState": vibreurState,
"camerafrontState": camerafrontState,
"camerabackState": camerabackState,
"boutonState": boutonState,
"usbState": usbState,
"kitState": kitState,
"geroscopeState": geroscopeState,
"empreinteState": empreinteState,
"captState": captState,
"greyState": greyState,
"proximiteState": proximiteState,
"lumiereState": lumiereState,
};
}
然后我寫了get方法來顯示數據; 它工作正常並且服務器發送數據但問題是在 flutter 中顯示數據它給了我這個異常
在構建 FutureBuilder>(dirty, state: _FutureBuilderState>#ba8ab) 時拋出了以下斷言:
類型“_TypeError”不是“字符串”類型的子類型
這是我的代碼
import 'dart:convert';
void main() => runApp((JsonDataParse()));
class JsonDataParse extends StatefulWidget {
@override
_JsonDataParseState createState() => _JsonDataParseState();
}
class _JsonDataParseState extends State<JsonDataParse> {
Future <List<Jsonclass>> getData() async{
final response = await http.get('http://192.168.43.24:27017/api/posts');
if(response.statusCode==200){
List phones =json.decode(response.body);
return phones.map((phone)=>new Jsonclass.fromJson(phone)).toList();
}
else {
throw Exception('Failed to load Users');
}
}
void initState(){
super.initState(); }
@override
Widget build(BuildContext context) {
return MaterialApp(
home:Scaffold(
appBar: AppBar(
title: Text('Mes Smartphones'),
),
body: Center(
child: FutureBuilder<List<Jsonclass>>(
future: getData(),
builder: (context,snapshot){
if(snapshot.hasData){
List<Jsonclass> phones =snapshot.data;
return new ListView(
children:phones.map((phone) =>Column(
children: <Widget>[
ListTile(
subtitle: Text('bonjour${phone.imei.name}'),
),
],
)).toList(),
);
}
else if(snapshot.hasError){
return Text(snapshot.error);
}
return new CircularProgressIndicator();
},
),
),
)
);
}
}
您的錯誤是因為您試圖將snapshot.error
放在Text
小部件中,並且它不是String
。
您可以通過將snapshot.error
轉換為String
來解決此問題。 檢查下面的代碼:它完美地工作:
Text(snapshot.error.toString())
我希望這有幫助。
您可能想嘗試使用 toString() 方法,這對我將字符串解析為文本小部件有很大幫助。 希望這會有所幫助
ListTile(
subtitle: Text('bonjour${phone.imei.name.toString()}'),
),
return Text(snapshot.error.toString());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.