Home > Back-end >  Flutter-GetX Error: The parameter 'index' can't have a value of 'null' beca
Flutter-GetX Error: The parameter 'index' can't have a value of 'null' beca

Time:02-24

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?

CodePudding user response:

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;

CodePudding user response:

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