在Flutter中解析JSON数据 – 无数据

huangapple 未分类评论48阅读模式
英文:

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应用程序或在屏幕截图中查看问题的具体情况,但根据你的描述,以下可能是问题的原因之一:

  1. 请确保你的API端点http://192.168.43.24:27017/api/posts正确地返回了有效的JSON数据。

  2. 确保JSON数据与PhoneTestImei类的结构匹配。任何属性名称或数据类型不匹配都可能导致解析失败。

  3. 检查你的网络连接,确保可以从应用程序中访问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 */
{
    &quot;_id&quot; : ObjectId(&quot;5e7c996fd6eb5f039c50bd26&quot;),
    &quot;imei&quot; : &quot;865566048694354&quot;,
    &quot;createdAt&quot; : ISODate(&quot;2020-03-26T12:00:47.021Z&quot;),
    &quot;updatedAt&quot; : ISODate(&quot;2020-03-26T12:00:47.021Z&quot;),
    &quot;__v&quot; : 0
}

/* 2 */
{
    &quot;_id&quot; : ObjectId(&quot;5ea8357d8c562b3dd8fe5bf1&quot;),
    &quot;imei&quot; : {
        &quot;test&quot; : {
            &quot;tactileState&quot; : &quot;ignore&quot;,
            &quot;pixelState&quot; : &quot;ignore&quot;
        },
        &quot;name&quot; : &quot;h12hhhhgkhh&quot;
    },
    &quot;createdAt&quot; : ISODate(&quot;2020-04-28T13:54:05.094Z&quot;),
    &quot;updatedAt&quot; : ISODate(&quot;2020-04-28T13:54:05.094Z&quot;),
    &quot;__v&quot; : 0
}

/* 3 */
{
    &quot;_id&quot; : ObjectId(&quot;5ea8366741a5e527446744a2&quot;),
    &quot;imei&quot; : {
        &quot;test&quot; : {
            &quot;tactileState&quot; : &quot;ignore&quot;,
            &quot;pixelState&quot; : &quot;ignore&quot;
        },
        &quot;name&quot; : &quot;h12hhhhgkhh&quot;
    },
    &quot;createdAt&quot; : ISODate(&quot;2020-04-28T13:57:59.035Z&quot;),
    &quot;updatedAt&quot; : ISODate(&quot;2020-04-28T13:57:59.035Z&quot;),
    &quot;__v&quot; : 0
}

/* 4 */
{
    &quot;_id&quot; : ObjectId(&quot;5ea837614cf7ed30f0163c38&quot;),
    &quot;imei&quot; : {
        &quot;test&quot; : {
            &quot;tactileState&quot; : &quot;ignore&quot;,
            &quot;pixelState&quot; : &quot;ignore&quot;,
            &quot;greyState&quot; : &quot;ignore&quot;
        },
        &quot;name&quot; : &quot;h12hhhhgkhh&quot;
    },
    &quot;createdAt&quot; : ISODate(&quot;2020-04-28T14:02:09.395Z&quot;),
    &quot;updatedAt&quot; : ISODate(&quot;2020-04-28T14:02:09.395Z&quot;),
    &quot;__v&quot; : 0
}

/* 5 */
{
    &quot;_id&quot; : ObjectId(&quot;5ea837854cf7ed30f0163c39&quot;),
    &quot;imei&quot; : {
        &quot;test&quot; : {
            &quot;bafleState&quot; : &quot;1&quot;,
            &quot;microState&quot; : &quot;1&quot;,
            &quot;vibreurState&quot; : &quot;1&quot;
        },
        &quot;name&quot; : &quot;h12hhhhgkhh&quot;
    },
    &quot;createdAt&quot; : ISODate(&quot;2020-04-28T14:02:45.287Z&quot;),
    &quot;updatedAt&quot; : ISODate(&quot;2020-04-28T14:02:45.287Z&quot;),
    &quot;__v&quot; : 0
}

/* 6 */
{
    &quot;_id&quot; : ObjectId(&quot;5ea8379a4cf7ed30f0163c3a&quot;),
    &quot;imei&quot; : {
        &quot;test&quot; : {
            &quot;bafleState&quot; : &quot;ignore&quot;,
            &quot;microState&quot; : &quot;ignore&quot;,
            &quot;vibreurState&quot; : &quot;ignore&quot;
        },
        &quot;name&quot; : &quot;h12hhhhgkhh&quot;
    },
    &quot;createdAt&quot; : ISODate(&quot;2020-04-28T14:03:06.156Z&quot;),
    &quot;updatedAt&quot; : ISODate(&quot;2020-04-28T14:03:06.156Z&quot;),
    &quot;__v&quot; : 0
}

/* 7 */
{
    &quot;_id&quot; : ObjectId(&quot;5ea837ad4cf7ed30f0163c3b&quot;),
    &quot;imei&quot; : {
        &quot;test&quot; : {
            &quot;boutonState&quot; : &quot;1&quot;
        },
        &quot;name&quot; : &quot;h12hhhhgkhh&quot;
    },
    &quot;createdAt&quot; : ISODate(&quot;2020-04-28T14:03:25.212Z&quot;),
    &quot;updatedAt&quot; : ISODate(&quot;2020-04-28T14:03:25.212Z&quot;),
    &quot;__v&quot; : 0
}

/* 8 */
{
    &quot;_id&quot; : ObjectId(&quot;5ea838d04cf7ed30f0163c3c&quot;),
    &quot;imei&quot; : {
        &quot;test&quot; : {
            &quot;boutonState&quot; : &quot;1&quot;,
            &quot;kitState&quot; : &quot;ignore&quot;,
            &quot;usbState&quot; : &quot;1&quot;,
            &quot;batterieState&quot; : &quot;0&quot;
        },
        &quot;name&quot; : &quot;h12hhhhgkhh&quot;
    },
    &quot;createdAt&quot; : ISODate(&quot;2020-04-28T14:08:16.383Z&quot;),
    &quot;updatedAt&quot; : ISODate(&quot;2020-04-28T14:08:16.383Z&quot;),
    &quot;__v&quot; : 0
}

/* 9 */
{
    &quot;_id&quot; : ObjectId(&quot;5ea8393c4cf7ed30f0163c3d&quot;),
    &quot;imei&quot; : {
        &quot;test&quot; : {
            &quot;boutonState&quot; : &quot;1&quot;,
            &quot;kitState&quot; : &quot;ignore&quot;,
            &quot;usbState&quot; : &quot;1&quot;,
            &quot;batterieState&quot; : &quot;Good&quot;
        },
        &quot;name&quot; : &quot;h12hhhhgkhh&quot;
    },
    &quot;createdAt&quot; : ISODate(&quot;2020-04-28T14:10:04.519Z&quot;),
    &quot;updatedAt&quot; : ISODate(&quot;2020-04-28T14:10:04.519Z&quot;),
    &quot;__v&quot; : 0
}

/* 10 */
{
    &quot;_id&quot; : ObjectId(&quot;5ea839754cf7ed30f0163c3e&quot;),
    &quot;imei&quot; : {
        &quot;test&quot; : {
            &quot;wifiState&quot; : &quot;0&quot;,
            &quot;gpsState&quot; : &quot;0&quot;
        },
        &quot;name&quot; : &quot;h12hhhhgkhh&quot;
    },
    &quot;createdAt&quot; : ISODate(&quot;2020-04-28T14:11:01.631Z&quot;),
    &quot;updatedAt&quot; : ISODate(&quot;2020-04-28T14:11:01.631Z&quot;),
    &quot;__v&quot; : 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&lt;String,dynamic&gt;json){
return Phone(
test: Test.fromJson(json [&#39;test&#39;]),
  imei: Imei.fromJson(json [&#39;imei&#39;]),);
}
}
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&lt;String,dynamic&gt;json){
return Test(
lumiereState: json [&#39;lumiereState&#39;],
proximiteState: json [&#39;proximiteState&#39;],
boutonState: json [&#39;boutonState&#39;],
bluetoothState: json [&#39;bluetoothState&#39;],
bafleState: json [&#39;bafleState&#39;],
batterieState: json [&#39;batterieState&#39;],
vibreurState: json [&#39;vibreurState&#39;],
camerabackState: json [&#39;camerabackState&#39;],
empreinteState: json [&#39;empreinteState&#39;],
usbState: json [&#39;usbState&#39;],
camerafrontState: json [&#39;camerafrontState&#39;],
wifiState: json [&#39;wifiState&#39;],
tactileState: json [&#39;tactileState&#39;],
pixelState: json [&#39;pixelState&#39;],
microState: json [&#39;microState&#39;],
gpsState: json [&#39;gpsState&#39;],
geroscopeState: json [&#39;geroscopeState&#39;],
captState: json [&#39;captState&#39;],
greyState: json [&#39;greyState&#39;],
kitState: json [&#39;kitState&#39;],


);
}
}

class Imei {
  String name;

  Imei({this.name});

  factory Imei.fromJson(Map&lt;String, dynamic&gt;json){
    return Imei(
      name: json [&#39;name&#39;],
    );
  }
}

This is the method of get query in flutter

 Future &lt;List&lt;Phone&gt;&gt; getData() async{
    final response = await http.get(&#39;http://192.168.43.24:27017/api/posts&#39;);
    if(response.statusCode==200){
      List phones =json.decode(response.body);
      return phones.map((phone)=&gt;new Phone.fromJson(
         
          phone)).toList();
    }
    else {
      throw Exception(&#39;Failed to load Users&#39;);
    }
  }

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(&#39;Mes Smartphones&#39;),
          ),
          body: Center(
            child: FutureBuilder&lt;List&lt;Phone&gt;&gt;(
              future: getData(),
              builder: (context,snapshot){
                if(snapshot.hasData){
                  List&lt;Phone&gt; phones =snapshot.data;
                  return new ListView(
                    children:phones.map((phone) =&gt;Column(
                      children: &lt;Widget&gt;[
                        ListTile(
                          subtitle: Text(&#39;bonjour${phone.imei.name}&#39;),
                        ),


                      ],
                    )).toList(),
                  );
                }
                else if(snapshot.hasError){
                return Column(children: &lt;Widget&gt;[
                  Text(snapshot.toString()),
                  Text(snapshot.hasData.toString()),
                   Text(snapshot.error.toString())]);
                }
                return new CircularProgressIndicator();
              },

always there is no data in the snapshot.

and that's a screenshot of what it returns:
在Flutter中解析JSON数据 – 无数据

huangapple
  • 本文由 发表于 2020年5月5日 20:30:23
  • 转载请务必保留本文链接:https://java.coder-hub.com/61613180.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定