首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >类型'List<dynamic>‘不是'Iterable<Map<dynamic,dynamic>>’类型的子类型

类型'List<dynamic>‘不是'Iterable<Map<dynamic,dynamic>>’类型的子类型
EN

Stack Overflow用户
提问于 2022-09-19 19:35:10
回答 2查看 115关注 0票数 0

我的代码面临一个问题。我知道错误:

代码语言:javascript
复制
type 'List<dynamic>' is not a subtype of type 'Iterable<Map<dynamic, dynamic>>'

这是我的密码:

代码语言:javascript
复制
 void getData() async {
    var url = 'http://xxxxxxx/getEvents.php';
    var res = await http.get(Uri.parse(url));
    var response = json.decode(res.body);
    print(response);

    var mySelectedEvents =
        groupBy(response, (Map obj) => obj['date']).map((k, v) => MapEntry(
            k,
            v.map((item) {
              item.remove('date');
              return item;
            }).toList()));
    print(mySelectedEvents);
  
  }

当我做print(response);的时候

代码语言:javascript
复制
[{id: 5, date: 2022-09-17, selectdate: 2022-09-17 22:13:04.508644, descript: azerty, title: azertyui, id_event_date: 4}, {id: 6, date: 2022-09-17, selectdate: 2022-09-17 23:19:05.885897, descript: 11, title: AZE, id_event_date: 5}, {id: 7, date: 2022-09-17, selectdate: 2022-09-17 23:19:05.885897, descript: 22, title: 4556, id_event_date: 6}, {id: 8, date: 2022-09-20, selectdate: 2022-09-20 00:00:00.000Z, descript: 77, title: HHJ, id_event_date: 7}, {id: 9, date: 2022-09-17, selectdate: 2022-09-17 23:20:46.357121, descript: 44, title: BYYY, id_event_date: 8}]
EN

回答 2

Stack Overflow用户

发布于 2022-09-19 20:23:38

这代码在我这边很好用。确保从正确的包导入groupBy方法:

代码语言:javascript
复制
import "package:collection/collection.dart";

并尝试将该类型添加到您的响应中:

代码语言:javascript
复制
List<Map<dynamic, dynamic>> response =

我认为这个答案解决了你的问题:Flutter/Dart how to groupBy list of maps

票数 0
EN

Stack Overflow用户

发布于 2022-09-19 20:52:38

用这个:

代码语言:javascript
复制
  var response = res.body as List;
  (response.map((e) async => await groupBy().fromMap(e))).toList()

将此替换为您的模型:

代码语言:javascript
复制
    // To parse this JSON data, do
//
//     final groupBy = groupByFromJson(jsonString);

import 'dart:convert';

class GroupBy {
    GroupBy({
        this.id,
        this.date,
        this.selectdate,
        this.descript,
        this.title,
        this.idEventDate,
    });

    final int id;
    final DateTime date;
    final DateTime selectdate;
    final String descript;
    final String title;
    final int idEventDate;

    factory GroupBy.fromRawJson(String str) => GroupBy.fromJson(json.decode(str));

    String toRawJson() => json.encode(toJson());

    factory GroupBy.fromJson(Map<String, dynamic> json) => GroupBy(
        id: json["id"] == null ? null : json["id"],
        date: json["date"] == null ? null : DateTime.parse(json["date"]),
        selectdate: json["selectdate"] == null ? null : DateTime.parse(json["selectdate"]),
        descript: json["descript"] == null ? null : json["descript"],
        title: json["title"] == null ? null : json["title"],
        idEventDate: json["id_event_date"] == null ? null : json["id_event_date"],
    );

    Map<String, dynamic> toJson() => {
        "id": id == null ? null : id,
        "date": date == null ? null : "${date.year.toString().padLeft(4, '0')}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}",
        "selectdate": selectdate == null ? null : "${selectdate.year.toString().padLeft(4, '0')}-${selectdate.month.toString().padLeft(2, '0')}-${selectdate.day.toString().padLeft(2, '0')}",
        "descript": descript == null ? null : descript,
        "title": title == null ? null : title,
        "id_event_date": idEventDate == null ? null : idEventDate,
    };
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73778462

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档