英文:
Flutter Parsing json Data - No data
问题
我正在使用Node.js和MongoDB在后端编写一个Flutter应用程序。我想进行一个GET查询。服务器成功发送数据,但是当我尝试显示数据时,快照中没有数据。以下是Robot 3T中显示的数据库内容:
/* 1 */
{
"_id" : ObjectId("5e7c996fd6eb5f039c50bd26"),
"imei" : "865566048694354",
"createdAt" : ISODate("2020-03-26T12:00:47.021Z"),
"updatedAt" : ISODate("2020-03-26T12:00:47.021Z"),
"__v" : 0
}
/* 2 */
{
"_id" : ObjectId("5ea8357d8c562b3dd8fe5bf1"),
"imei" : {
"test" : {
"tactileState" : "ignore",
"pixelState" : "ignore"
},
"name" : "h12hhhhgkhh"
},
"createdAt" : ISODate("2020-04-28T13:54:05.094Z"),
"updatedAt" : ISODate("2020-04-28T13:54:05.094Z"),
"__v" : 0
}
...
// 后续数据也类似
以下是我用于解析JSON数据的类:
class Phone {
Imei imei;
Test test;
Phone({this.test, this.imei});
factory Phone.fromJson(Map<String, dynamic> json) {
return Phone(
test: Test.fromJson(json['test']),
imei: Imei.fromJson(json['imei']),
);
}
}
class Test {
// 属性列表
Test({/* 属性列表 */});
factory Test.fromJson(Map<String, dynamic> json) {
return Test(
// 解析属性
);
}
}
class Imei {
String name;
Imei({this.name});
factory Imei.fromJson(Map<String, dynamic> json) {
return Imei(
name: json['name'],
);
}
}
这是在Flutter中执行GET查询的方法:
Future<List<Phone>> 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) => Phone.fromJson(phone)).toList();
} else {
throw Exception('Failed to load Users');
}
}
这是在应用程序中显示数据的方法:
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Mes Smartphones'),
),
body: Center(
child: FutureBuilder<List<Phone>>(
future: getData(),
builder: (context, snapshot) {
if (snapshot.hasData) {
List<Phone> phones = snapshot.data;
return ListView(
children: phones.map((phone) => Column(
children: <Widget>[
ListTile(
subtitle: Text('Bonjour ${phone.imei.name}'),
),
],
)).toList(),
);
} else if (snapshot.hasError) {
return Column(
children: <Widget>[
Text(snapshot.toString()),
Text(snapshot.hasData.toString()),
Text(snapshot.error.toString()),
],
);
}
return CircularProgressIndicator();
},
),
),
),
);
}
然而,你提到快照中没有数据。我无法执行Flutter应用程序或在屏幕截图中查看问题的具体情况,但根据你的描述,以下可能是问题的原因之一:
-
请确保你的API端点
http://192.168.43.24:27017/api/posts
正确地返回了有效的JSON数据。 -
确保JSON数据与
Phone
、Test
和Imei
类的结构匹配。任何属性名称或数据类型不匹配都可能导致解析失败。 -
检查你的网络连接,确保可以从应用程序中访问API端点。
如果问题仍然存在,我建议你逐步检查代码并进行调试,以查找导致数据没有正确显示的原因。
英文:
I'm writing a flutter application using node js and mongoDb in the back end. I want to make a get query. It works the server send data, but when I try to display data I did not find anything, the snapshot has no data. That's the database shown in robot 3T
/* 1 */
{
"_id" : ObjectId("5e7c996fd6eb5f039c50bd26"),
"imei" : "865566048694354",
"createdAt" : ISODate("2020-03-26T12:00:47.021Z"),
"updatedAt" : ISODate("2020-03-26T12:00:47.021Z"),
"__v" : 0
}
/* 2 */
{
"_id" : ObjectId("5ea8357d8c562b3dd8fe5bf1"),
"imei" : {
"test" : {
"tactileState" : "ignore",
"pixelState" : "ignore"
},
"name" : "h12hhhhgkhh"
},
"createdAt" : ISODate("2020-04-28T13:54:05.094Z"),
"updatedAt" : ISODate("2020-04-28T13:54:05.094Z"),
"__v" : 0
}
/* 3 */
{
"_id" : ObjectId("5ea8366741a5e527446744a2"),
"imei" : {
"test" : {
"tactileState" : "ignore",
"pixelState" : "ignore"
},
"name" : "h12hhhhgkhh"
},
"createdAt" : ISODate("2020-04-28T13:57:59.035Z"),
"updatedAt" : ISODate("2020-04-28T13:57:59.035Z"),
"__v" : 0
}
/* 4 */
{
"_id" : ObjectId("5ea837614cf7ed30f0163c38"),
"imei" : {
"test" : {
"tactileState" : "ignore",
"pixelState" : "ignore",
"greyState" : "ignore"
},
"name" : "h12hhhhgkhh"
},
"createdAt" : ISODate("2020-04-28T14:02:09.395Z"),
"updatedAt" : ISODate("2020-04-28T14:02:09.395Z"),
"__v" : 0
}
/* 5 */
{
"_id" : ObjectId("5ea837854cf7ed30f0163c39"),
"imei" : {
"test" : {
"bafleState" : "1",
"microState" : "1",
"vibreurState" : "1"
},
"name" : "h12hhhhgkhh"
},
"createdAt" : ISODate("2020-04-28T14:02:45.287Z"),
"updatedAt" : ISODate("2020-04-28T14:02:45.287Z"),
"__v" : 0
}
/* 6 */
{
"_id" : ObjectId("5ea8379a4cf7ed30f0163c3a"),
"imei" : {
"test" : {
"bafleState" : "ignore",
"microState" : "ignore",
"vibreurState" : "ignore"
},
"name" : "h12hhhhgkhh"
},
"createdAt" : ISODate("2020-04-28T14:03:06.156Z"),
"updatedAt" : ISODate("2020-04-28T14:03:06.156Z"),
"__v" : 0
}
/* 7 */
{
"_id" : ObjectId("5ea837ad4cf7ed30f0163c3b"),
"imei" : {
"test" : {
"boutonState" : "1"
},
"name" : "h12hhhhgkhh"
},
"createdAt" : ISODate("2020-04-28T14:03:25.212Z"),
"updatedAt" : ISODate("2020-04-28T14:03:25.212Z"),
"__v" : 0
}
/* 8 */
{
"_id" : ObjectId("5ea838d04cf7ed30f0163c3c"),
"imei" : {
"test" : {
"boutonState" : "1",
"kitState" : "ignore",
"usbState" : "1",
"batterieState" : "0"
},
"name" : "h12hhhhgkhh"
},
"createdAt" : ISODate("2020-04-28T14:08:16.383Z"),
"updatedAt" : ISODate("2020-04-28T14:08:16.383Z"),
"__v" : 0
}
/* 9 */
{
"_id" : ObjectId("5ea8393c4cf7ed30f0163c3d"),
"imei" : {
"test" : {
"boutonState" : "1",
"kitState" : "ignore",
"usbState" : "1",
"batterieState" : "Good"
},
"name" : "h12hhhhgkhh"
},
"createdAt" : ISODate("2020-04-28T14:10:04.519Z"),
"updatedAt" : ISODate("2020-04-28T14:10:04.519Z"),
"__v" : 0
}
/* 10 */
{
"_id" : ObjectId("5ea839754cf7ed30f0163c3e"),
"imei" : {
"test" : {
"wifiState" : "0",
"gpsState" : "0"
},
"name" : "h12hhhhgkhh"
},
"createdAt" : ISODate("2020-04-28T14:11:01.631Z"),
"updatedAt" : ISODate("2020-04-28T14:11:01.631Z"),
"__v" : 0
}
and that's the class which I use to parse JSON data
class Phone{
Imei imei;
Test test;
Phone({this.test,this.imei});
factory Phone.fromJson(Map<String,dynamic>json){
return Phone(
test: Test.fromJson(json ['test']),
imei: Imei.fromJson(json ['imei']),);
}
}
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.lumiereState,this.proximiteState,this.bluetoothState,this.batterieState,this.usbState,this.kitState,this.greyState,this.bafleState,this.boutonState,this.camerabackState,this.camerafrontState,this.captState,this.empreinteState,this.geroscopeState,this.gpsState,this.microState,this.pixelState,this.tactileState,this.vibreurState,this.wifiState});
factory Test.fromJson(Map<String,dynamic>json){
return Test(
lumiereState: json ['lumiereState'],
proximiteState: json ['proximiteState'],
boutonState: json ['boutonState'],
bluetoothState: json ['bluetoothState'],
bafleState: json ['bafleState'],
batterieState: json ['batterieState'],
vibreurState: json ['vibreurState'],
camerabackState: json ['camerabackState'],
empreinteState: json ['empreinteState'],
usbState: json ['usbState'],
camerafrontState: json ['camerafrontState'],
wifiState: json ['wifiState'],
tactileState: json ['tactileState'],
pixelState: json ['pixelState'],
microState: json ['microState'],
gpsState: json ['gpsState'],
geroscopeState: json ['geroscopeState'],
captState: json ['captState'],
greyState: json ['greyState'],
kitState: json ['kitState'],
);
}
}
class Imei {
String name;
Imei({this.name});
factory Imei.fromJson(Map<String, dynamic>json){
return Imei(
name: json ['name'],
);
}
}
This is the method of get query in flutter
Future <List<Phone>> 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 Phone.fromJson(
phone)).toList();
}
else {
throw Exception('Failed to load Users');
}
}
When I check the server I found that the method works and the server send data and that's the method to display data in the app.
@override
Widget build(BuildContext context) {
return MaterialApp(
home:Scaffold(
appBar: AppBar(
title: Text('Mes Smartphones'),
),
body: Center(
child: FutureBuilder<List<Phone>>(
future: getData(),
builder: (context,snapshot){
if(snapshot.hasData){
List<Phone> 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 Column(children: <Widget>[
Text(snapshot.toString()),
Text(snapshot.hasData.toString()),
Text(snapshot.error.toString())]);
}
return new CircularProgressIndicator();
},
always there is no data in the snapshot.
专注分享java语言的经验与见解,让所有开发者获益!
评论