在颤振中处理实体初始值的最佳方法是什么

时间:2021-03-08 17:27:34

标签: flutter

当我像这样在 flutter(2.0.1) 中定义一个对象时:

Item article = Item();

它告诉我应该给出这样的默认参数:

  Item article = Item(pubTime: null, id: '', dead: null);

但是Item有这么多字段,所以代码会很长而且看起来很丑。这是定义的项目:

class Item {
  Item({
    this.depth = 0,
    required this.author,
    required this.deleted,
    required this.content,
    required this.dead,
    required this.poll,
    required this.parent,
    required this.parts,
    required this.descendants,
    required this.id,
    required this.kids,
    required this.score,
    required this.pubTime,
    required this.title,
    required this.type,
    required this.link,
    required this.isFav,
    required this.favCount,
    required this.subSourceId,
    required this.isUpvote,
    required this.upvoteCount
  });

  int depth;
  String author;
  bool deleted;
  String content;
  bool dead;
  int poll;
  int parent;
  List<int> parts;
  int descendants;
  String id;
  List<int> kids;
  int score;
  int pubTime;
  String title;
  StoryType type;
  String link;
  int isFav;
  int favCount;
  int isUpvote;
  int upvoteCount;
  String subSourceId;

  factory Item.fromJson(String str) => Item.fromMap(json.decode(str));

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

  bool isVoted() => HistoryManager.isVoted(id);

  String get domain => Uri.parse(link).host;
  String get ago =>
      timeago.format(DateTime.fromMillisecondsSinceEpoch(pubTime));

  factory Item.fromMap(Map<String, dynamic> json) => Item(
    id: json["id"],
    author: json["author"] == null ? "" : json["author"],
    deleted: json["deleted"] == null ? false : json["deleted"],
    content: json["content"] == null ? "" : json["content"],
    dead: json["dead"] == null ? false : json["dead"],
    poll: json["poll"] == null ? null : json["poll"],
    parent: json["parent"] == null ? null : json["parent"],
    parts: json["parts"] == null
        ? []
        : List<int>.from(json["parts"].map((x) => x)),
    descendants: json["descendants"] == null ? 0 : json["descendants"],
    kids: json["kids"] == null
        ? []
        : List<int>.from(json["kids"].map((x) => x)),
    score: json["score"] == null ? 0 : json["score"],
    pubTime: json["pubTime"] == null ? 0 : json["pubTime"],
    title: json["title"] == null ? "" : json["title"],
    type: json["type"] == null ? StoryType.job : castType(json["type"]),
    link: json["link"] == null ? "" : json["link"],
    isFav: json["isFav"] == null ? "" : json["isFav"],
    favCount: json["favCount"] == null ? "" : json["favCount"],
    isUpvote: json["isUpvote"] == null ? "" : json["isUpvote"],
    upvoteCount: json["upvoteCount"] == null ? "" : json["upvoteCount"],
    subSourceId: json["subSourceId"] == null ? "" : json["subSourceId"],
  );

  Map<String, dynamic> toMap() => {
    "id": id,
    "author": author == null ? null : author,
    "deleted": deleted == null ? null : deleted,
    "content": content == null ? null : content,
    "dead": dead == null ? null : dead,
    "poll": poll == null ? null : poll,
    "parent": parent == null ? null : parent,
    "parts": parts == null ? null : List<dynamic>.from(parts.map((x) => x)),
    "descendants": descendants == null ? null : descendants,
    "kids": kids == null ? null : List<dynamic>.from(kids.map((x) => x)),
    "score": score == null ? null : score,
    "pubTime": pubTime == null ? null : pubTime,
    "title": title == null ? null : title,
    "type": type == null ? null : type,
    "link": link == null ? null : link,
    "isFav": isFav == null ? null : isFav,
    "favCount": favCount == null ? null : favCount,
    "isUpvote": isUpvote == null ? null : isUpvote,
    "upvoteCount": upvoteCount == null ? null : upvoteCount,
    "subSourceId": subSourceId == null ? null : subSourceId,
  };

  static StoryType castType(String type) {
    switch (type) {
      case "job":
        return StoryType.job;
        break;
      case "story":
        return StoryType.story;
        break;
      case "comment":
        return StoryType.comment;
        break;
      case "poll":
        return StoryType.poll;
        break;
      case "pollopt":
        return StoryType.pollopt;
        break;
      default:
        return StoryType.job;
        break;
    }
  }
}

我应该怎么做才能更好地为 Item 提供默认值?我不想这样定义:

Item? article;

因为我不想要带有空值异常的文章。

2 个答案:

答案 0 :(得分:1)

你应该根据你在你的类中实现的逻辑工作,如果你定义了一个带有 REQUIRED 参数的构造函数,那么你应该期望系统引发警告。如果您不想要这些警告,那么您应该创建一个空的构造函数:Item(); 并使用默认值初始化您的变量或为每种数据类型设置 ?

答案 1 :(得分:1)

这确实取决于您的很多需求,但是,您不能将所有属性都标记为 required,而不是使整个 Item 可以为空,而是可以:

class Item {
class Item {
  Item(
      {this.depth = 0, required this.author, this.dead = false, this.pubTime}) {
    this.pubTime ??= DateTime.now().microsecondsSinceEpoch;
  }

  int depth;
  String author;
  bool dead;
  int? pubTime;
}
}

根据您的业务逻辑和要求,默认情况下始终将 item dead 属性声明为 false,将日期设置为当前时间,并始终要求作者姓名,这对您来说很有意义。 出于测试目的,您可以为您的项目创建一个小型工厂构造函数,该构造函数只需要几个参数,例如:

  factory Item.test() => Item(
        author: "Peter",
        dead: true,
        depth: 42,
    ...// All other attributes
      );

我建议您阅读更多有关此 article

中的 dart 构造函数的信息