我很傻,我正在环顾四周,我看不到基本选项的任何选项,例如TypeScript。
请查看评论的财产。 '用户名'。
我定义了我的模型。
type User {
uid: string,
// Not chosen yet. The team knows this might be null from the '?' Might never be chosen.
username?: string
accountType: 'email'|'facebook' // Also no union types in Dart?
}
我怎么能在Dart中实现同样的标记编译时警告/错误的东西?
如果我们像user.?username这样的条件展开也会很好。
Swift,Java,TypeScript,Flow,C#都有这个。它非常方便。
我写道:
class User {
final String uid;
final String username;
final AccountType accountType;
User(this.uid, this.userName, this.accountType) {
ArgumentError.checkNotNull(uid, "uid");
ArgumentError.checkNotNull(accountType, "accountType");
}
}
enum AccountType { email, facebook; }
Dart还没有非可空类型,因此您必须手动检查null。您无法获得编译时警告。我们希望引入非可空类型作为默认的“很快”,此时您应该能够编写String? userName;
。您已经可以使用user?.userName?.toUpperCase()
有条件地调用可能是null
的值的方法。
Dart没有联合类型,但你在这里使用的是枚举类型,Dart确实具有。它们不能像Java的枚举类型那样复杂,但是对于两个值之间的简单选择,它们就足够了。