skip to Main Content

I have a model that returns data as shown below in a List:

[ GoodsModel(id: 483, productCategory: sport, productType: t-shirt, productDisplayName: Shirt, product_key: sport_goods, productRecommendation: null, sport_goods: {id: 12, name: t-shirt colored, color: blue}) ]

The value of the field product_key (sport_goods) matches with a map sport_goods
My question is, please, how can I extract only that piece of map ‘sport_goods’ based on the product_key?

var myMap = goodsList.map((e) {
        if (e. product_key == 'sport_goods') {
          return ???;
        }
      });

I am not sure how the return should look like. Any help please?

EDIT: I am starting to work on it. My ultimate goal is to have a list of widgets that will be created from the items returned from the piece of map. Like this:

import 'package:flutter/material.dart';

class GoodsList extends StatefulWidget {
  const GoodsList({super.key});

  @override
  State<GoodsList> createState() => _GoodsListState();
}

class _GoodsListState extends State<GoodsList> {
  // Data sample
  var data = {
    "id": 483,
    "productCategory": "sport",
    "productType": "t-shirt",
    "productDisplayName": "Shirt",
    "product_key": "sport_goods", // (2) based on this value
    "productRecommendation": null,
  // (1) I need to get this data
    "sport_goods": {
      "id": 12,
      "name": "t-shirt colored",
      "color": "blue",
    }
  };

  @override
  Widget build(BuildContext context) {
    for (var i = 0; i < data.length; i++) {
      // (3) how to get the piece of map in here???
      if (data[i]!['product_key'] == 'sport_goods') {
        return Column(
          children: [
            Text(data[i]!['sport_goods']!['id']),
            Text(data[i]!['sport_goods']!['name']),
            Text(data[i]!['sport_goods']!['color']),
          ],
        ) // This is the problem I believe
      }
    }

    return const Text('No data');
  }
}

2

Answers


  1.  var myMap = {};
      for (var goods in goodsList) {
        if (goods.productKey == 'sport_goods') {
          myMap = goods.sport_goods;
        }
      }
    

    You can use above code snippet to retrieve the map stored in sport_goods key.

    Here, we are initializing myMap variable.
    Then we are searching the goodsList where productKey is sports_good.
    And finally storing it in myMap.

    Login or Signup to reply.
  2. I think you want to do something like this?

    https://dartpad.dev/?id=9c0421e4a18b873fd5ec5e404ee57289

    I’ll paste the code here below so you can see – but the dartpad link should show you the output too.

    class _GoodsListState extends State<GoodsList> {
      // Data sample
      var data = {
        "id": 483,
        "productCategory": "sport",
        "productType": "t-shirt",
        "productDisplayName": "Shirt",
        "product_key": "sport_goods",
        "productRecommendation": null,
        "sport_goods": {
          "id": 12,
          "name": "t-shirt colored",
          "color": "blue",
        }
      };
    
      @override
      Widget build(BuildContext context) {
        return Column(
          children:
            ((data['product_key'] ?? "") == "sport_goods") ?
              switch (data["sport_goods"]){
                {"id":int id, "name": String name, "color": String color} => [Text(id.toString()), Text(name), Text(color)],
                 _ => [const Text('No Data')]
              }
              :[const Text('No Data')]
      }
    }
    

    The first part of the children is a check to see if the product_key == sport_goods, and if so, uses pattern matching to create the children list of Text widgets.

    If the pattern matching fails, or if the product_key != sport_goods, in both cases we return a list of a single Text with "No Data" as the content.

    Hopefully this helps you

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search