简体   繁体   中英

How to show Data coming from API in Grouped List View

I have data coming from api like this:

   {
        "status": true,
        "error": "",
        "message": "Record Found.",
        "data": {
            "2021-Apr-19": [
                {
                    "img_url": "https://www.rezzap.com/uploads/profile-photo/BWPOFCyHMFm_OgzvCxRwS5dBJTS6wCgy.jpg",
                    "person": "CooperMac",
                    "message": " supported your activity drawing."
                }
            ],
            "2021-Mar-30": [
                {
                    "img_url": "https://www.rezzap.com/uploads/profile-photo/BWPOFCyHMFm_OgzvCxRwS5dBJTS6wCgy.jpg",
                    "person": "CooperMac",
                    "message": " commented on your activity Test Activity 12-11-2020."
                },
              {
                "img_url": "https://www.rezzap.com/uploads/profile-photo/BWPOFCyHMFm_OgzvCxRwS5dBJTS6wCgy.jpg",
                "person": "CooperMac",
                "message": " supported your activity ."
            },
            ],
    }
    }

for same I have made Model Like this -:

class Support {
  Support({
    this.data,
  });

  Map<String, List<Datum>> data;

  factory Support.fromJson(Map<String, dynamic> json) => Support(
        data: Map.from(json["data"]).map((k, v) =>
            MapEntry<String, List<Datum>>(
                k, List<Datum>.from(v.map((x) => Datum.fromJson(x))))),
      );
}

class Datum {
  Datum({
    this.imgUrl,
    this.person,
    this.message,
  });

  String imgUrl;
  String person;
  String message;

  factory Datum.fromJson(Map<String, dynamic> json) => Datum(
        imgUrl: json["img_url"],
        person: json["person"],
        message: json["message"],
      );
}

and Here is the response coming But I don't know How to parse it in model, I need corrections here so if Anyone can help

 Future<Support> getSupportActivityData() async {
    final response = await _helper.get(supportedActivityUrl);
    var responseData = response["data"];
    var list = responseData as Map<String, dynamic>;
    return Support.fromJson(list);
  }

I tried to give return type as "Future<Map<String, List>>" and others too but it always show me Type cast error

I want to get that data and show it as List , so how can I show it here in this grouped List as It asks for key as Header and values as its row?

try this code to convert the map you have first to list

and this can happen from The Backend response or you can convert it from The flutter by

var h=here_the_map_data.forEach((k,v) => here_The_list_variable.add(here_the_map_data(k,v)));

I replaced my function with this where response was coming -:

  Future<Support> getSupportActivityData() async {
    final response = await _helper.get(supportedActivityUrl);
    return Support.fromJson(response);
  }

I already parsed ["data"] in model and what i was doing again parsing data in my repo(where response was coming)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM