flutter: Variable can't be used as a setter because it's final

Viewed 6672
List<TestModel> lists = List();

FutureBuilder<List<TestModel>>(
          future: testNetworkRepository.fetchAlltext(TestModel.testKey),
          builder: (context, snapshot){
            if(snapshot.hasData){
              lists = snapshot.data;
              return Contanier();
            }
          }
)

Future _editText(int index, String testKey) async {
    await showDialog(
        context: context,
        child: SimpleDialog(
          children: [
            SimpleDialogOption(
              child: TextButton(
                    child: Text("Edit"),
                    onPressed: (){
                      setState(() {
                        lists[index].text = editTextController.text; <- error occured
                      });
                    },
                  ),
            )
          ],
        )
    );
  }

This is my code. I want to edit lists[index].text.

But error occured.

'text' can't be used as a setter because it's final.

How can I solve this problem?

2 Answers

This error happened because the text property of the TestModel class is final. Final objects arent meant to be changed in Flutter. to fix this problem you should go to the TestModel class and remove final from the text property.

Go in data class model change it like below:

static late List<item> items

if they not working then remove final keyword.

Related