Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

flutter

dart

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?

like image 841
K.k Avatar asked Oct 29 '25 13:10

K.k


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.

like image 107
biniyam112 Avatar answered Nov 01 '25 11:11

biniyam112


Go in data class model change it like below:

static late List<item> items

if they not working then remove final keyword.

like image 41
Muhammad Nawaz Avatar answered Nov 01 '25 10:11

Muhammad Nawaz