Accessing quicktype JSON object in flutter

1,185

greetings and instructions are part of the "sample" json that quicktype shows by default.

The json you shared is totally different and it doesn't contain instructions property so it will never find it.

Share:
1,185
futureExpert
Author by

futureExpert

Master suppression techniques don't compile. In coding, proper argumentation always wins.

Updated on December 02, 2022

Comments

  • futureExpert
    futureExpert over 1 year

    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<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();
        });
      }
    
    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}");
                        }
                  ),
                  ),
    

    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)