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



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

Item article = Item();

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

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

但是Item有很多字段,所以代码会很长,看起来很难看。这是定义的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;

因为我不希望文章有空值异常。

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

这真的取决于你的很多需求,但是,而不是使整个项目为空,你不能标记所有属性为required,相反,你可以:

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;
}
}

根据您的业务逻辑和需求,在默认情况下总是声明一个条目的死亡属性为false,将日期设置为当前时间,并始终要求作者名称(这一切都取决于),这对您来说是有意义的。出于测试目的,您可以为您的项目创建一个小型工厂构造函数,它只接受几个参数,例如:

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

我建议您阅读更多关于dart构造函数的内容,如本文

所示。

最新更新