Я хочу сделать запрос get с некоторыми json данными. Я написал класс, используя этот веб-сайт https://app.quicktype.io/, и он дал мне этот код
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 для отображения данных; он работал правильно, и сервер отправлял данные, но проблема была в отображении данных во флаттере, он дал мне это исключение
Следующее утверждение было брошено при построении FutureBuilder> (dirty, state: _FutureBuilderState> # ba8ab):
тип '_TypeError' не является подтипом типа 'String'
, и это мой код
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();
},
),
),
)
);
}
}