Flutter-GetX Error: The parameter 'index' can't have a value of 'null' because of its type, but the implicit default value is 'null'
Flutter-GetX Error: The parameter 'index' can't have a value of 'null' because of its type, but the implicit default value is '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});
constructor 现在很好但是
text = todoController.todos[index].text;
必须遵循错误
The argument type 'int?' can't be assigned to the parameter type 'int'.
有人能找出这里有什么问题吗?
您的代码
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;
}
更新代码
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;
}
出现这个错误是因为null_safety想告诉你这个int可以为null,但是在你想使用它的地方是绝对必要的,这意味着:int必须有一个值
您可以通过输入 '!' 来解决问题在索引名称后
text = todoController.todos[index!].text;
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});
constructor 现在很好但是
text = todoController.todos[index].text;
必须遵循错误
The argument type 'int?' can't be assigned to the parameter type 'int'.
有人能找出这里有什么问题吗?
您的代码
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;
}
更新代码
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;
}
出现这个错误是因为null_safety想告诉你这个int可以为null,但是在你想使用它的地方是绝对必要的,这意味着:int必须有一个值
您可以通过输入 '!' 来解决问题在索引名称后
text = todoController.todos[index!].text;