在flutter中访问quicktype JSON对象

fut*_*ert 6 parsing json quicktype flutter

我有一个 JSON 字符串,该字符串使用 quicktype 中生成的代码映射到“Pax”的实例中。Quicktype 生成了大约 4000 行代码映射,所以我很高兴并相信它在某种程度上是有效的。我现在想打印这海量数据的一小部分作为开​​始。这是一个位于 pax.instructions.id 的字符串。

final String paxRaw = response.body;
final Pax xa = paxFromJson(paxRaw);
Run Code Online (Sandbox Code Playgroud)
    import 'dart:convert';
    
    Pax paxFromJson(String str) => Pax.fromJson(json.decode(str));
    
    String paxToJson(Pwa data) => json.encode(data.toJson());
    
    class Pax {
      Pax({
        this.greeting,
        this.instructions,
      });
    
      String greeting;
      List<Instruction> instructions;
    
      factory Pax.fromRawJson(String str) => Pax.fromJson(json.decode(str));
    
      String toRawJson() => json.encode(toJson());
    
      factory Pax.fromJson(Map<String, dynamic> json) => Pax(
        greeting: json["greeting"] == null ? null : json["greeting"],
        instructions: json["instructions"] == null ? null : List<Instruction>.from(json["instructions"].map((x) => Instruction.fromJson(x))),
      );
    
      Map<String, dynamic> toJson() => {
        "greeting": greeting == null ? null : greeting,
        "instructions": instructions == null ? null : List<dynamic>.from(instructions.map((x) => x.toJson())),
      };
    }
Run Code Online (Sandbox Code Playgroud)

我想访问名为id的列表指令的数据成员。

print(xa);
Run Code Online (Sandbox Code Playgroud)

返回控制台:

I/flutter ( 4535): Instance of 'Pax'
Run Code Online (Sandbox Code Playgroud)

我知道指令是一个列表,但是如何访问该列表中名为 id 的字符串?我最好的猜测是 print(xa.instructions<id>);但它不起作用。显然已经构建了一些东西,但我不知道如何在调试级别(在 android studio 中)检查“xa”。有助于指导。

更新,还是不行

  Future<Pax> _futurePax;

  Future<Pax> getPax() async {
    debugPrint("getPax start");
[...]
    http.Response response = await http.get(baseUri);
    debugPrint('Response status: ${response.statusCode}');
    debugPrint(response.body);
    return Pax.fromJson(json.decode(response.body));
  }
  @override
  void initState(){
    super.initState();
    setState(() {
      _futurePax = getPax();
    });
  }
Run Code Online (Sandbox Code Playgroud)
Container (
                child: FutureBuilder<Pax> (
                    future: _futurePax,
                    builder: (context, snapshot) {
                      debugPrint("Futurebuilder<Pax> buildpart");
                      debugPrint("Test snapshot content: ${snapshot.data.toString()}");
                      debugPrint("Test snapshot error: ${snapshot.error}");
                      debugPrint("Test snapshot has data (bool): ${snapshot.hasData}");
                      debugPrint(snapshot.data.instructions[0].id);
                      return Text("Snap: ${snapshot.data.instructions[0].id}");
                    }
              ),
              ),
Run Code Online (Sandbox Code Playgroud)

安慰:

Syncing files to device sdk gphone x86...
I/flutter ( 5126): Futurebuilder<Pax> buildpart
I/flutter ( 5126): Test snapshot content: Instance of 'Pax'
I/flutter ( 5126): Test snapshot error: null
I/flutter ( 5126): Test snapshot has data (bool): true

???????? Exception caught by widgets library ???????????????????????????????????????????????????????
The following NoSuchMethodError was thrown building FutureBuilder<Pax>(dirty, state: _FutureBuilderState<Pax>#a2168):
The method '[]' was called on null.
Receiver: null
Tried calling: [](0)
Run Code Online (Sandbox Code Playgroud)