Flutter-GetX 错误:参数“index”;值不能为“null”;因为它的类型,但隐式默认值为“null”;

发布于 2025-01-09 10:34:51 字数 2350 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

弃爱 2025-01-16 10:34:51

您的代码

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;
    
    }

Your Code

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;

}

update code

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;
    
    }
魄砕の薆 2025-01-16 10:34:51

发生这个错误是因为 null_safety 想要告诉你这个 int 可以为 null,但是在你想要使用它的地方是绝对必要的,这意味着:int 必须有一个值

你可以通过放置一个 '!' 来解决问题在索引名称之后

text = todoController.todos[index!].text;

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

text = todoController.todos[index!].text;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文