Flutter-GetX 错误:参数“index”;值不能为“null”;因为它的类型,但隐式默认值为“null”;
1 我对 Dart 和一般编码都很陌生。我在 YouTube 上观看教程后编写了这段代码。在大多数情况下,我能够自己解决大部分问题,但我无法找出我最近的错误。当我尝试纠正无状态小部件中的变量时,会发生以下错误。
The parameter 'index' can't have a value of 'null' because of its type, but the implicit default value is 'null'.
代码是:
class TodoScreen extends StatelessWidget {
final TodoController todoController = Get.find();
int index;
TodoScreen({this.index});
@override
Widget build(BuildContext context) {
String? text ='';
if(this.index != null){
text = todoController.todos[index].text;
}
TextEditingController textEditingController = TextEditingController();
return Scaffold(
body: Container(
padding: const EdgeInsets.all(20.0),
child: Column(
children: [
Expanded(
child: TextField(
controller: textEditingController,
autofocus: true,
decoration: const InputDecoration(
hintText: 'What do you want to accomplish',
border: InputBorder.none,
focusedBorder: InputBorder.none),
style: TextStyle(fontSize: 25.0),
keyboardType: TextInputType.multiline,
maxLines: 999,
),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
ElevatedButton(
onPressed: () {Get.back();},
child: const Text("Cancle"),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.red),
),
),
ElevatedButton(
onPressed: () {
todoController.todos.add(Todo(text: textEditingController.text), ); Get.back();
},
child: const Text("Add"),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.green),
),
)
],
),
],
),
),
);
}
[![enter image description here][1]][1]}
当我更改
final TodoController todoController = Get.find();
int? index;
TodoScreen({this.index});
构造函数时,现在可以了,但
text = todoController.todos[index].text;
必须遵循错误
The argument type 'int?' can't be assigned to the parameter type 'int'.
,有人可以找到这里出了什么问题吗?
1
I am very new to Dart, and coding in general. I have produced this code after watching tutorials on YouTube. For the most part, I have been able to troubleshoot most of my problems on my own, yet I cannot figure out my most recent errors. When I try to right the variable in a stateless widget the following error occurs.
The parameter 'index' can't have a value of 'null' because of its type, but the implicit default value is 'null'.
the code is:
class TodoScreen extends StatelessWidget {
final TodoController todoController = Get.find();
int index;
TodoScreen({this.index});
@override
Widget build(BuildContext context) {
String? text ='';
if(this.index != null){
text = todoController.todos[index].text;
}
TextEditingController textEditingController = TextEditingController();
return Scaffold(
body: Container(
padding: const EdgeInsets.all(20.0),
child: Column(
children: [
Expanded(
child: TextField(
controller: textEditingController,
autofocus: true,
decoration: const InputDecoration(
hintText: 'What do you want to accomplish',
border: InputBorder.none,
focusedBorder: InputBorder.none),
style: TextStyle(fontSize: 25.0),
keyboardType: TextInputType.multiline,
maxLines: 999,
),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
ElevatedButton(
onPressed: () {Get.back();},
child: const Text("Cancle"),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.red),
),
),
ElevatedButton(
onPressed: () {
todoController.todos.add(Todo(text: textEditingController.text), ); Get.back();
},
child: const Text("Add"),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.green),
),
)
],
),
],
),
),
);
}
[![enter image description here][1]][1]}
and when I change
final TodoController todoController = Get.find();
int? index;
TodoScreen({this.index});
constructor is fine now but
text = todoController.todos[index].text;
has to following error
The argument type 'int?' can't be assigned to the parameter type 'int'.
can someone please find what is wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码
更新代码
Your Code
update code
发生这个错误是因为 null_safety 想要告诉你这个 int 可以为 null,但是在你想要使用它的地方是绝对必要的,这意味着:int 必须有一个值
你可以通过放置一个 '!' 来解决问题在索引名称之后
this error occurs because null_safety wants to tell you that this int can be null, but is absolutely necessary here where you want to use it, that means: the int must have a value
you can solve the problem by putting a '!' after the index name