@require参数"#"不能具有"null"的值,因为它的类型



大家好,在一个项目中工作的每个人,从repo中获得了这些代码,并且有一些类型的错误,我无法理解它们,因为我没有解决这些错误的知识,而且我在谷歌上也没有找到任何关于这些错误的信息。

问题是@需要这个#属性,错误为null值。我不能理解这个问题,能给我解释一下这个问题吗?

主页小工具

class Home extends StatelessWidget {
const Home({
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: [
Container(
decoration: const BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
AppColors.backgroundFadedColor,
AppColors.backgroundColor,
],
stops: [0.0, 1],
),
),
),
SafeArea(
child: _TodoListContent(
todos: fakeData,
),
),
const Align(
alignment: Alignment.bottomRight,
child: AddTodoButton(),
)
],
),
);
}
}
class _TodoListContent extends StatelessWidget {
const _TodoListContent({
Key? key,
@required this.todos,
}) : super(key: key);
final List<Todo> todos;
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: todos.length,
padding: const EdgeInsets.all(16),
itemBuilder: (context, index) {
final _todo = todos[index];
return _TodoCard(todo: _todo);
},
);
}
}
class _TodoCard extends StatelessWidget {
const _TodoCard({
Key? key,
@required this.todo,
}) : super(key: key);
final Todo todo;
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
Navigator.of(context).push(
HeroDialogRoute(
builder: (context) => Center(
child: _TodoPopupCard(todo: todo),
),
),
);
},
child: Hero(
tag: todo.id,
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0),
child: Material(
color: AppColors.cardColor,
borderRadius: BorderRadius.circular(12),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: <Widget>[
_TodoTitle(title: todo.description),
const SizedBox(
height: 8,
),
if (todo.items.length != 0) ...[
const Divider(),
_TodoItemsBox(items: todo.items),
]
],
),
),
),
),
),
);
}
}
class _TodoTitle extends StatelessWidget {
const _TodoTitle({
Key? key,
@required this.title,
}) : super(key: key);
final String title;
@override
Widget build(BuildContext context) {
return Text(
title,
style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
);
}
}
class _TodoPopupCard extends StatelessWidget {
const _TodoPopupCard({Key key, this.todo}) : super(key: key);
final Todo todo;
@override
Widget build(BuildContext context) {
return Hero(
tag: todo.id,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Material(
borderRadius: BorderRadius.circular(16),
color: AppColors.cardColor,
child: SizedBox(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
_TodoTitle(title: todo.description),
const SizedBox(
height: 8,
),
if (todo.items.length != 0) ...[
const Divider(),
_TodoItemsBox(items: todo.items),
],
Container(
margin: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: Colors.black12,
borderRadius: BorderRadius.circular(8),
),
child: const TextField(
maxLines: 8,
cursorColor: Colors.white,
decoration: InputDecoration(
contentPadding: EdgeInsets.all(8),
hintText: 'Write a note...',
border: InputBorder.none),
),
),
],
),
),
),
),
),
),
);
}
}
class _TodoItemsBox extends StatelessWidget {
const _TodoItemsBox({
Key? key,
@required this.items,
}) : super(key: key);
final List<Item> items;
@override
Widget build(BuildContext context) {
return Column(
children: [
for (final item in items) _TodoItemTile(item: item),
],
);
}
}
class _TodoItemTile extends StatefulWidget {
const _TodoItemTile({
Key? key,
@required this.item,
}) : super(key: key);
final Item item;
@override
_TodoItemTileState createState() => _TodoItemTileState();
}
class _TodoItemTileState extends State<_TodoItemTile> {
void _onChanged(bool val) {
setState(() {
widget.item.completed = val;
});
}
@override
Widget build(BuildContext context) {
return ListTile(
leading: Checkbox(
onChanged: _onChanged,
value: widget.item.completed,
),
title: Text(widget.item.description),
);
}
}

关于类属性@required this.#错误:The parameter '#' 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.

型号文件

import 'package:meta/meta.dart';
class Todo {
const Todo({
@required this.id,
@required this.description,
this.items,
});
final String id;
final String description;
final List<Item> items;
}
class Item {
Item({
@required this.id,
this.description = '',
this.completed = false,
});
final String id;
final String description;
bool completed;
}

代码

Todo
@required this.id,
@required this.description,
this.items,

Item
@required this.id,

错误:The parameter '#' 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.

TLDR:更改@required=>required

您正在使用";零安全";已启用。这是一件好事,并且通过在编译时捕获错误来帮助避免错误。

Todo中,您有一个字段final String id;。启用空安全时,null而不是String的有效值(即String id = null;是编译时错误(。

这意味着您可以安全地调用id上的方法,而不必担心它是null,但这也意味着您必须给它一个值。

考虑:

final todo = Todo();
print(todo.id);  // what happens here?

如果编译器允许您的代码,这将打印";空";。但是idString,而不是String?(一个可以为null的字符串(,所以这是不允许的。

您面临的主要问题是@required的使用,而不仅仅是required@required是一个元数据注释,它允许开发工具(例如IDE(发出有用的警告。

另一方面,required是一个语言关键字(当启用空安全时(。如果您有一个非null命名参数,则必须其中之一:

  1. 将其标记为required,因此如果未能提供它,则为编译时错误
  2. 给它一个默认值,这样即使您不提供它,它也将是非null的

相关内容

最新更新