【问题标题】:The argument type 'Id?' can't be assigned to the parameter type 'Id'参数类型“Id?”不能分配给参数类型“Id”
【发布时间】:2023-03-09 21:56:01
【问题描述】:

不知道怎么解决。这两行显示错误。运行代码应该解决了。

我在将类型 id 和名称转换为“id”和“名称”的两行出现错误。

// To parse this JSON data, do
//
//     final welcome = welcomeFromJson(jsonString);

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.status,
    required this.totalResults,
    required this.articles,
  });

  String status;
  int totalResults;
  List<Article> articles;

  factory Welcome.fromJson(Map<String, dynamic> json) => Welcome(
        status: json["status"],
        totalResults: json["totalResults"],
        articles: List<Article>.from(
            json["articles"].map((x) => Article.fromJson(x))),
      );

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

class Article {
  Article({
    required this.source,
    required this.author,
    required this.title,
    required this.description,
    required this.url,
    required this.urlToImage,
    required this.publishedAt,
    required this.content,
  });

  Source source;
  String author;
  String title;
  String description;
  String url;
  String urlToImage;
  DateTime publishedAt;
  String content;

  factory Article.fromJson(Map<String, dynamic> json) => Article(
        source: Source.fromJson(json["source"]),
        author: json["author"] == null ? null : json["author"],
        title: json["title"],
        description: json["description"],
        url: json["url"],
        urlToImage: json["urlToImage"],
        publishedAt: DateTime.parse(json["publishedAt"]),
        content: json["content"],
      );

  Map<String, dynamic> toJson() => {
        "source": source.toJson(),
        "author": author == null ? null : author,
        "title": title,
        "description": description,
        "url": url,
        "urlToImage": urlToImage,
        "publishedAt": publishedAt.toIso8601String(),
        "content": content,
      };
}

class Source {
  Source({
    required this.id,
    required this.name,
  });

  Id id;
  Name name;

  factory Source.fromJson(Map<String, dynamic> json) => Source(
        id:  idValues.map[json["id"]],
        name: nameValues.map[json["name"]],
      );

  Map<String, dynamic> toJson() => {
        "id": idValues.reverse[id],
        "name": nameValues.reverse[name],
      };
}

enum Id { THE_WALL_STREET_JOURNAL }

final idValues =
    EnumValues({"the-wall-street-journal": Id.THE_WALL_STREET_JOURNAL});

enum Name { THE_WALL_STREET_JOURNAL }

final nameValues =
    EnumValues({"The Wall Street Journal": Name.THE_WALL_STREET_JOURNAL});

class EnumValues<T> {
  Map<String, T> map;
  late Map<T, String> reverseMap;

  EnumValues(this.map);

  Map<T, String> get reverse {
    if (reverseMap == null) {
      reverseMap = map.map((k, v) => new MapEntry(v, k));
    }
    return reverseMap;
  }
}

我认为如果我们解析它们,那么它可以解决,但我不知道如何解析它。

【问题讨论】:

    标签: flutter dart dart-null-safety


    【解决方案1】:

    您已将 Id 设置为必需,但您的代码不能保证您正在解析的内容实际上将具有一个不为空的 Id 值,因为您使用的是具有动态值的 Map,这意味着它们可以是任何东西,包括空值。换句话说:您不能将一个可为空的 Id(“Id?”后面的“?”表示它可以为空)分配给一个不可为空的 Id。

    在不运行您的代码的情况下,一些可能有帮助的建议是:

    • 使您的 Id 属性可以为空
    • 确保在您尝试使用“!”分配 id 时该值不为空。这 ”!”表示您保证该值不为空。
    • 在声明后使用“late”关键字初始化值。

    但请注意:如果您不能保证该值不为 null,则后两个建议将导致异常,从而违背了 null 安全的全部目的。

    【讨论】:

      【解决方案2】:

      更改这些代码:

        Id id;
        Name name;
      

      到这里:

        Id? id;
        Name? name;
      

      【讨论】:

        猜你喜欢
        • 2022-08-18
        • 2021-01-05
        • 2021-12-03
        • 2018-12-26
        • 2021-06-19
        • 2021-12-13
        • 2021-08-19
        • 2020-06-25
        • 2019-02-13
        相关资源
        最近更新 更多