【问题标题】:flutter typeerror on json list sortjson列表排序上的颤振类型错误
【发布时间】:2019-09-24 16:15:24
【问题描述】:

尝试对解码后的 json 进行排序时出错:

发生了异常。 _TypeError (type '(dynamic, dynamic) => dynamic' 不是 >type '(dynamic, dynamic) => int' of 'compare' 的子类型)"

json 看起来像这样: https://imgur.com/a/BdU3THE

我猜sort(),它在body中有一个compareTo,返回int(-1, 1),也就是'dynamic, dynamic) => int'部分,解码后的json是一个动态的Map by默认'动态,动态)=>动态',所以我需要投一些东西。但是我看不到我的代码中的哪个函数正在返回动态?或者即使这是问题所在?

      if (localData != null && localData["articles"] != null) {

the error occurs on the next line:

      localData["articles"].toList().sort((a, b) => 
       a["publishedAt"] != null && b["publishedAt"] != null
              ? b["publishedAt"].compareTo(a["publishedAt"])
              : null);
    }

【问题讨论】:

    标签: json sorting flutter typeerror


    【解决方案1】:

    我为您创建了一个示例,其中有 Categories 列表并添加了 publishedAt 字段对其进行排序

    这是json:

    {
      "Categories": [
        {
          "id": 1,
          "name": "Restruants",
          "publishedAt": "2019-05-07T00:36:38Z",
        },
        {
          "id": 2,
          "name": "Car Rental",
          "publishedAt": "2019-06-07T00:36:38Z",
        },
        {
          "id": 3,
          "name": "Furniture",
          "publishedAt": "2019-12-21T00:36:38Z",
        },
        {
          "id": 4,
          "name": "cars",
          "publishedAt": "2019-08-10T00:36:38Z",
        },
        {
          "id": 5,
          "name": "Maintenance",
          "publishedAt": "2019-03-15T00:36:38Z",
        },
        {
          "id": 6,
          "name": "Education",
          "publishedAt": "2019-09-17T00:36:38Z",
        },
        {
          "id": 7,
          "name": "Finess",
          "publishedAt": "2019-01-28T00:36:38Z",
        },
        {
          "id": 8,
          "name": "Electronics",
          "publishedAt": "2019-09-19T00:36:38Z",
        },
        {
          "id": 9,
          "name": "Medical",
          "publishedAt": "2019-12-25T00:36:38Z",
        },
        {
          "id": 10,
          "name": "Entirtainment",
          "publishedAt": "2019-06-14T00:36:38Z",
        }
      ]
    }
    

    这是模型类,当我们从json中获取数据时,您可以在其中找到排序方法。

    您可以在“// TO SORT ARRAY WITH publishedAt”下面的注释中看到该方法开始

    class ItemModel {
      List<Category> categories;
    
      ItemModel({
        this.categories,
      });
    
    
      factory ItemModel.fromJson(Map<String, dynamic> json){
    
        var accJson = json["Categories"] as List;
        List<Category> accList = accJson.map((i) => Category.fromJson(i)).toList();
    
    
        // TO SORT ARRAY WITH publishedAt
        accList.sort((a, b) {
          return a.publishedAt.compareTo(b.publishedAt);
        });
    
        return ItemModel(
            categories: accList
        );
      }
    
      Map<String, dynamic> toJson() => {
        "Categories": new List<dynamic>.from(categories.map((x) => x.toJson())),
      };
    }
    
    class Category {
      int id;
      String name;
      String publishedAt;
      IconPath iconPath;
    
      Category({
        this.id,
        this.name,
        this.publishedAt,
        this.iconPath,
      });
    
      factory Category.fromJson(Map<String, dynamic> json) => new Category(
        id: json["id"],
        name: json["name"],
        publishedAt: json["publishedAt"],
        iconPath: iconPathValues.map[json["iconPath"]],
      );
    
      Map<String, dynamic> toJson() => {
        "id": id,
        "name": name,
        "publishedAt": publishedAt,
        "iconPath": iconPathValues.reverse[iconPath],
      };
    }
    

    下面是从资产中获取静态 json 文件列表的 Future 类,它会给你排序结果,因为我们是 已经在“fromJson”方法中对其进行了排序:

    Future<ItemModel> fetchMovieList(BuildContext context) async {
        final jsonCategory = await DefaultAssetBundle
            .of(context)
            .loadString('assets/CategoryList.json');
    
        Map<String, dynamic> values = Map<String, dynamic>.from(jsonDecode(jsonCategory));
    
        final mapJsonCategory = Map<String, dynamic>.from(values);
    
        print(mapJsonCategory);
        return ItemModel.fromJson(mapJsonCategory);
      }
    

    希望对你有帮助:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-22
      • 1970-01-01
      • 2022-08-23
      • 2022-11-28
      • 2019-12-29
      • 2021-06-09
      • 1970-01-01
      • 2021-10-21
      相关资源
      最近更新 更多