Pea*_*Gen 2 android ios dart flutter dart-null-safety
我正在构建 Flutter 应用程序的模型类。我之前构建过很多 Flutter 应用程序,但这是我第一次接触 Flutter 2.0。我的班级如下。
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})
}
Run Code Online (Sandbox Code Playgroud)
但是,我的每个参数都出现错误,如下所示。
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}
Run Code Online (Sandbox Code Playgroud)
我知道我可以添加required标签和更多内容。但大多数时候这些数据都是从数据库中提取的,所以我不能确切地说哪一个是空的。然而,从数据库端来看,只有first_name和email被定义为not-null字段。
我应该在这里做什么?
尝试像下面这样改变。
(并且你最好将蛇形变量名称更改为 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});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2673 次 |
| 最近记录: |