根据可为空值创建可为空对象
我必须创建一个具有可为空属性的域类对象。这个创建是原始类的扩展。
例如:
Meeting({required String id, required Datetime date, Example? example})
Example后面的Domainclass是一个像这样的类:Example(String value)
MeetingPrimitive.dart:
extension MeetingPrimitiveX on Meeting {
Meeting toDomain() {
return Meeting(
id: id,
date: date,
example: example == null ? null : Example(example)
}
}
我的问题是,我如何简化它:
example == null ? null : Example(example)
'''
thanks in advance
i have to create a domain class object which has a nullable attribute. This Creating is in an extansion of a primitive class.
For example: Domainclass
Meeting({required String id, required Datetime date, Example? example})
behind Example is an class like so: Example(String value)
MeetingPrimitive.dart:
extension MeetingPrimitiveX on Meeting {
Meeting toDomain() {
return Meeting(
id: id,
date: date,
example: example == null ? null : Example(example)
}
}
My question is, how can i simplify this:
example == null ? null : Example(example)
'''
thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一般情况下你不能。
您可以通过各种方式更改它,但没有什么比这里更直接的了。
一种变体是:
这样,您可以编写:
如果
example
是非则有条件地调用
Example.new
构造函数(剥离未命名的构造函数) >空。稍微短一点,需要一个辅助函数,并且......实际上在我看来并不更具可读性。
You generally can't.
You can change it in various ways, but nothing is more direct than what you have here.
One variant would be:
With that, you can write:
to conditionally call the
Example.new
constructor (tear-off of the unnamed constructor) ifexample
is non-null
.Slightly shorter, requires a helper function, and ... isn't actually more readable IMO.