如何处理模型 类 中的 NULL SAFETY?
How to handle NULL SAFETY in model classes?
我正在构建我的 Flutter 应用程序的模型 classes。我以前构建过很多 Flutter 应用程序,但这是我第一次接触 Flutter 2.0。我的 class 如下所示。
import 'package:json_annotation/json_annotation.dart';
@JsonSerializable()
class User {
int iduser;
String uid;
String first_name;
String last_name;
String profile_picture;
String email;
String phone;
bool is_disabled;
int created_date;
int last_updated;
User({this.iduser,
this.uid,
this.first_name,
this.last_name,
this.profile_picture,
this.email,
this.is_disabled,
this.created_date,
this.last_updated})
}
但是我收到每个参数的错误,如下所示。
The parameter 'iduser' can't have a value of 'null' because of its type, but the implicit default value is 'null'.
Try adding either an explicit non-'null' default value or the 'required' modifier.dart(missing_default_value_for_parameter)
{int iduser}
我知道我可以添加 required
标签等等。但大多数时候这些数据会从数据库中拉取,所以我不能确切地说出哪一个是null。但是,从数据库端来看,只有 first_name
和 email
被定义为 not-null
字段。
我应该在这里做什么?
尝试像下面这样改变。
(而且你最好将 snake case 变量名改为 lowerCamelCase)
https://dart.dev/guides/language/effective-dart/style#do-name-other-identifiers-using-lowercamelcase
class User {
int? iduser;
String? uid;
String? first_name;
String? last_name;
String? profile_picture;
String? email;
String? phone;
bool? is_disabled;
int? created_date;
int? last_updated;
User(
{this.iduser,
this.uid,
this.first_name,
this.last_name,
this.profile_picture,
this.email,
this.is_disabled,
this.created_date,
this.last_updated});
}
我正在构建我的 Flutter 应用程序的模型 classes。我以前构建过很多 Flutter 应用程序,但这是我第一次接触 Flutter 2.0。我的 class 如下所示。
import 'package:json_annotation/json_annotation.dart';
@JsonSerializable()
class User {
int iduser;
String uid;
String first_name;
String last_name;
String profile_picture;
String email;
String phone;
bool is_disabled;
int created_date;
int last_updated;
User({this.iduser,
this.uid,
this.first_name,
this.last_name,
this.profile_picture,
this.email,
this.is_disabled,
this.created_date,
this.last_updated})
}
但是我收到每个参数的错误,如下所示。
The parameter 'iduser' can't have a value of 'null' because of its type, but the implicit default value is 'null'.
Try adding either an explicit non-'null' default value or the 'required' modifier.dart(missing_default_value_for_parameter)
{int iduser}
我知道我可以添加 required
标签等等。但大多数时候这些数据会从数据库中拉取,所以我不能确切地说出哪一个是null。但是,从数据库端来看,只有 first_name
和 email
被定义为 not-null
字段。
我应该在这里做什么?
尝试像下面这样改变。
(而且你最好将 snake case 变量名改为 lowerCamelCase)
https://dart.dev/guides/language/effective-dart/style#do-name-other-identifiers-using-lowercamelcase
class User {
int? iduser;
String? uid;
String? first_name;
String? last_name;
String? profile_picture;
String? email;
String? phone;
bool? is_disabled;
int? created_date;
int? last_updated;
User(
{this.iduser,
this.uid,
this.first_name,
this.last_name,
this.profile_picture,
this.email,
this.is_disabled,
this.created_date,
this.last_updated});
}