最近,dart 发布了名为 null safety 的新功能。我对如何创建带有命名参数的构造函数有点困惑。当我遵循花括号的使用时,出现错误。
这是我的代码:
void main() {
}
class Student {
String name = '';
num age = 0;
List<num> marks = [];
Student({
this.name,
this.age,
this.marks
});
void printStudentDetails() {
print('Student Name: ' + this.name);
}
}
Run Code Online (Sandbox Code Playgroud)
我正在构建 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 …
在《Flutter Apprentice First Edition》一书中,我有以下代码:
import 'package:flutter/painting.dart';
// 1
enum Importance { low, medium, high }
class GroceryItem {
// 2
final String id;
// 3
final String name;
final Importance importance;
final Color color;
final int quantity;
final DateTime date;
final bool isComplete;
...
Run Code Online (Sandbox Code Playgroud)
构建方法为
Widget buildDate() {
final dateFormatter = DateFormat('MMMM dd h:mm a');
final dateString = dateFormatter.format(item!.date);
return Text(
dateString,
style: TextStyle(decoration: textDecoration),
);
}
Run Code Online (Sandbox Code Playgroud)
我有以下错误:
参数类型“日期时间?” 无法分配给参数类型“DateTime”。
我已经研究 flutter 几个月了,我开始了解如何解决 null 安全问题,但这对我来说真的很复杂,我希望得到帮助。
这是我的问题:
每当我尝试登录时,都会收到此错误: 'Null' 类型不是 Map<string,dynamic> 类型的子类型 我尽力找出问题所在,但我无法,任何帮助都将是拯救生命...
import 'package:cloud_firestore/cloud_firestore.dart';
class User {
final String email;
final String uid;
final String photoUrl;
final String username;
final String bio;
final List followers;
final List following;
const User({
required this.email,
required this.uid,
required this.photoUrl,
required this.username,
required this.bio,
required this.followers,
required this.following,
});
Run Code Online (Sandbox Code Playgroud)
然后我继续在这里
Map<String, dynamic> toJson() => {
"username": username,
"uid": uid,
"email": email,
"photoUrl": photoUrl,
"bio": bio,
"followers": followers,
"following": following,
};
static User fromSnap(DocumentSnapshot snap) …Run Code Online (Sandbox Code Playgroud) 我注意到 Dart/Flutter 标签上的更多用户尝试在较新的 Dart SDK 版本中尝试空安全,我开始阅读它,从这篇 Medium 文章开始。
我注意到在他们所有的例子中,他们都使用了位置性的、必需的参数。但是空安全如何与可选参数一起工作,包括位置参数和命名参数?
可选参数本质上是null这样的,这是否意味着所有可选参数都必须使用启用空安全的可空变量声明语法来声明?添加 似乎只是一个小小的不便?,但它可能会破坏大量使用可选参数的代码。dart 是否能够对可选参数进行例外处理(知道它们总是可以为空的),从而可以避免如此大的更改?或者是否有更简单的替代方法可以使我的代码与空安全兼容以避免这些更改?
我正在使用新的空安全类型对 Dart 类进行建模。我相信有两种有效的方法来初始化从参数计算的不可空属性。
对于本例,我们将使用Favorite 类。
此类在构造函数中使用初始值设定项列表。
class Favourite {
int favouriteId;
Favourite({required this.favouriteId});
Favourite.mapFromJson(dynamic json)
: this.favouriteId = json["favouriteId"];
}
Run Code Online (Sandbox Code Playgroud)
此类使用“late”关键字。
class Favourite {
late int favouriteId;
Favourite({required this.favouriteId});
Favourite.mapFromJson(dynamic json) {
this.favouriteId = json["favouriteId"];
}
}
Run Code Online (Sandbox Code Playgroud)
你什么时候会使用其中一种而不是另一种?使用“迟到”感觉有风险。如果我添加另一个命名构造函数,编译器不会抱怨“favouriteId”未初始化。
还有其他选择吗?
谢谢你!
如果过期不为空,我想渲染文本小部件。所以我检查了 null
但出现错误
class TileButton extends StatelessWidget {
final DateTime? expires;
const TileButton({
Key? key,
this.expires,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Row(
children: [
Text(
'Hello Flutter',
),
if (expires != null)
Text(
expires.day.toString(),
),
],
);
}
}
Run Code Online (Sandbox Code Playgroud) 我创建了一个类,并希望某些参数是可选的,但不知道该怎么做。
class PageAction {
PageState state;
PageConfiguration page;
List<PageConfiguration> pages;
Widget widget;
PageAction({
this.state = PageState.none,
this.page, // Optional
this.pages, // Optional
this.widget, // Optional
});
Run Code Online (Sandbox Code Playgroud)
我收到添加“必需”的建议,但这不是我需要的。有人可以帮忙解释一下吗?
constructor optional-parameters flutter flutter-navigation dart-null-safety
我在 Stateful 类中声明了一个回调方法,例如:
final void Function(int index)? onSelected;
MyBottomNavigationBar({@required this.onSelected});
Run Code Online (Sandbox Code Playgroud)
并在 state 类中使用widget.onselected调用,例如:
widget.onSelected!(_selectedIndex);
Run Code Online (Sandbox Code Playgroud)
但我无法理解! 砰运算符。我不能在没有给出的情况下在构造函数中初始化 widget.onSelected ?申报期间。
我的主要问题是“如何! bang 运算符处理空值?它有什么用?