Home > Software design >  type 'Null' is not a subtype of type 'String' flutter / dart
type 'Null' is not a subtype of type 'String' flutter / dart

Time:03-16

I have this json and i want to show variation's keys and values (i should extract keys and values because they're not stable).

{
  "data" : [
     "items": {
                "388488": {
                    "id": 388488,
                    "name": "Galaxy Tab A7 10.4 2020",
                    "qty": 1,
                    "variations": {
                        "color": "green",
                                   },
                           },
                "388489": {
                    "id": 388489,
                    "name": "Samsung Galaxy Tab A7 10.4 2020",
                    "qty": 1,
                    "variations": {
                        "color": "red",
                        "something": "somth"
                                   },
                },
                "388490": {
                    "id": 388490,
                    "name": " Apple iPad 10.2",
                    "qty": 1,
                    "variations": {
                        "color": "blue",
                        "something1": "somth1"
                                   },
                }
            }
       ]
 }

when i print variation in this way it is fine and print all variations :

  itemDetail.variations.forEach((key, value) {
       print(value);
     });

but when i try to show it in a text widget it says variation is null :

                  Text(
                            itemDetail.variations.forEach((key , value){
                              String Description = "$key : $value";
                              print(Description); // it just print the variation of first item
                              return Description; // I also tried without return, same error
                            }),
                            textDirection: TextDirection.rtl,),

itemDetail is an object of Item model class here it is:

class Items{
  late final id;
  late final name;
  late final quantity;
  late final variations;
  Items({
   this.id,
   this.name,
   this.quantity,
    this.variations
});
  
  
  factory Items.fromJson(Map<String , dynamic> Json){
    return Items(
      id: Json["id"],
      name: Json["name"],
      quantity: Json["qty"],
        variations: Json["variations"] ?? ""
    );
  }
}

CodePudding user response:

Instead of forEach try map because forEach doesn't return anything.

 Text(
       itemDetail.variations.map((key , value){
       String Description = "$key : $value";
       print(Description); // it just print the variation of first item
      return Description; // I also tried without return, same error
      }),
     textDirection: TextDirection.rtl,),

CodePudding user response:

You probably want your listview to be something like

ListView(
        children: itemDetail.variations.entries
            .map((entry) => Text("${entry.key} : ${entry.value}"))
            .toList());
  • Related