【问题标题】:NoSuchMethodError : The method 'map' was called on null. Plz Provide the SolutionNoSuchMethodError :在 null 上调用了方法“map”。请提供解决方案
【发布时间】:2023-03-08 02:07:01
【问题描述】:

我正在尝试从 Dart/Flutter 应用程序中的 Rest API 解析数据。 JSON 在根处包含一个名为 data 的字段,其中包含单词列表。 我想从这个 JSON 中得到一个List<ArticalList>,给出json["data"].map((x) => ArticalList.fromJson(x))

我已经有以下代码:

import 'dart:convert';

Welcome welcomeFromJson(String str) => Welcome.fromJson(json.decode(str));

String welcomeToJson(Welcome data) => json.encode(data.toJson());

class Welcome {
  Welcome({
    required this.code,
    required this.status,
    required this.message,
    required this.data,
  });

  final int code;
  final String status;
  final String message;
  final List<ArticalList> data;

  factory Welcome.fromJson(Map<String, dynamic> json) => Welcome(
        code: json["code"] ?? 0,
        status: json["status"] ?? '',
        message: json["message"] ?? '',
        data: List<ArticalList>.from(json["data"].map((x) => ArticalList.fromJson(x))),
      );

  Map<String, dynamic> toJson() => {
        "code": code,
        "status": status,
        "message": message,
        "data": List<dynamic>.from(data.map((x) => x.toJson())),
      };
}

class ArticalList {
  ArticalList({
    required this.id,
    required this.title,
    required this.detail,
    required this.image,
  });

  int id;
  String title;
  String detail;
  String image;

  factory ArticalList.fromJson(Map<String, dynamic> json) => ArticalList(
        id: json["id"] == null ? 0 : json["id"],
        title: json["title"] == null ? '' : json["title"],
        detail: json["detail"] == null ? '' : json["detail"],
        image: json["image"] ?? 'http://eduteksolutions.in/images/logo.jpeg',
      );

  Map<String, dynamic> toJson() => {
        "id": id == null ? null : id,
        "title": title == null ? null : title,
        "detail": detail == null ? null : detail,
        "image": image,
      };
}

【问题讨论】:

  • 你能告诉我们上面代码中抛出错误的那一行吗?哪个地图函数抛出错误?
  • 你能提供你要解析的json吗
  • 数据:列表.from( json["data"].map((x) => ArticalList.fromJson(x))), );
  • data:json["data"]==null?[]: List.from( json["data"].map((x) => ArticalList.fromJson(x) )),我检查 null 但仍然返回 Data List is Empty

标签: json flutter rest dictionary dart


【解决方案1】:

我认为你的错误是

data: List<ArticalList>.from(json["data"].map((x) => ArticalList.fromJson(x))),

我创建了一个具有 null 安全性的函数,它接受映射并返回对象列表

static List<ArticalList> toListFormMap({
    Map? map,
  }) {
    if (map?.isEmpty ?? true) return [];
    List<ArticalList> items = [];
    map?.forEach((key, data) {
      final ArticalList item = ArticalList.fromJson(data);
      items.add(item);
    });
    return items;
  }

与将地图转换为地图的方法相同

static Map toMapList(List<ArticalList>? items) {
    Map map = {};
    items?.forEach((element) {
      map[element.id] = element.toJson();
    });
    return map;
  }

对于这两种情况,它都会处理空数据错误,并将对象列表转换为映射列表,将映射列表转换为对象列表。 我希望它会有所帮助。

【讨论】:

    猜你喜欢
    • 2019-12-23
    • 2020-01-25
    • 2021-10-25
    • 2019-07-09
    • 1970-01-01
    • 2020-05-23
    • 1970-01-01
    • 2020-12-08
    • 2021-11-30
    相关资源
    最近更新 更多