0

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 the 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,),

item detail is an object of the 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"] ?? ""
    );
  }
}
7
  • what do you exactly mean with "says variation is null"? your comment says it does print the first variation or not? Commented Mar 16, 2022 at 9:06
  • also it seems weird to iterate over the variations inside the Text. You probably would want to have a separate Text for each variation Commented Mar 16, 2022 at 9:08
  • @IvoBeckers yes it does print the first one , but i need to extract the other variation's keys and values too, not just the first one. Commented Mar 16, 2022 at 9:08
  • @IvoBeckers I have generated a list view and i'll show every variation in a card , i know how to do that it's not problem Commented Mar 16, 2022 at 9:10
  • Then I don't understand why you even have a forEach inside the Text. Shouldn't there just be a single variation? It's logically that your code only shows the first. I don't know what you are trying to do Commented Mar 16, 2022 at 9:15

2 Answers 2

1

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,),
Sign up to request clarification or add additional context in comments.

Comments

0

You probably want your listview to be something like

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.