android - 解析复杂json数据时出现 flutter 问题

标签 android ios json flutter dart

我想使用一些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方法来显示数据;它工作正常,服务器发送数据,但是问题在于在抖动中显示数据,这给了我这个异常(exception)

The following assertion was thrown building FutureBuilder>(dirty, state: _FutureBuilderState>#ba8ab):
type '_TypeError' is not a subtype of type '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();
          },
        ),
      ),
    )
    );
  }
}

最佳答案

您可能想尝试使用toString()方法,这对将字符串解析为Text Widgets很有帮助。希望这会有所帮助

 ListTile(
   subtitle: Text('bonjour${phone.imei.name.toString()}'),
 ),
return Text(snapshot.error.toString());

关于android - 解析复杂json数据时出现 flutter 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61589768/

相关文章:

Android popBackStackImmediate 动画返回堆栈 fragment

ios - 如何在 swift 的单例中使用此代码?

ios - 二元运算符 '<<' 不能应用于两个 'T' 操作数

json - 比较两个 JSON 文件

html - 如何在 ng-repeat 中允许重复?

c# - 创建特定格式 Json 的正确方法

android - 如何访问非 Activity 类中的上下文

java - Android模糊动态/滚动内容

android - 我怎样才能在android中播放来自byte的视频

ios - 通过蜂窝连接连接时出现 SSL 错误