#StackBounty: #json #parsing #flutter #quicktype Accessing quicktype JSON object in flutter

Bounty: 50

I have a JSON string that is mapped with code generated in quicktype, into an instance of "Pax". Quicktype generated some 4000 rows of code mapping this so Im happy and confident that it works to some extent. I now want to print a specific small part of this sea of data to start with. It’s a string located at pax.instructions.id.

final String paxRaw = response.body;
final Pax xa = paxFromJson(paxRaw);
    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())),
      };
    }

I want to access a data member of the list instructions that is called id.

print(xa);

Returns console:

I/flutter ( 4535): Instance of 'Pax'

I know instructions is a list, but how do I acess the string that is called id in this list? My best guess is
print(xa.instructions<id>); but it doesn’t work. There’s clearly something built, but I can’t figure out how to inspect "xa" on a debug level (in android studio). Helpful for guidance.

UPDATE, still not working

  Future<Pwa> _futurePwa;

  Future<Pwa> getPwa() async {
    debugPrint("getPwa start");
[...]
    http.Response response = await http.get(baseUri);
    debugPrint('Response status: ${response.statusCode}');
    debugPrint(response.body);
    return Pwa.fromJson(json.decode(response.body));
  }
  @override
  void initState(){
    super.initState();
    setState(() {
      _futurePwa = getPwa();
    });
  }
Container (
                child: FutureBuilder<Pwa> (
                    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}");
                    }
              ),
              ),

Console:

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)


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.