0

I want to program a shopping list app it throws me from line 41 the debug error

"UnsupportedError (Unsupported operation: Cannot add to an unmodifiable list)".

Source of the Add Button

ElevatedButton(
                  onPressed: () {
                    shoppingList.add(ShoppingItem(
                        name: _textEditingController.text, done: false));
                    Navigator.pop(context);
                  },
                  child: const Text('Add')
                  ),

the whole source code of the AddShoppingItemScreen

import 'package:flutter/material.dart';
import 'package:shoppinglist/screens/shopping_item.dart';
import 'config.dart';

class AddShoppingItemScreen extends StatefulWidget {
  const AddShoppingItemScreen({Key? key}) : super(key: key);

  @override
  State<AddShoppingItemScreen> createState() => _AddShoppingItemScreenState();
}

class _AddShoppingItemScreenState extends State<AddShoppingItemScreen> {
  final TextEditingController _textEditingController =
      TextEditingController(text: '');

  @override
  Widget build(BuildContext context) {
    final shoppingList = context
        .dependOnInheritedWidgetOfExactType<Configuration>()!
        .shoppingList;

    return Scaffold(
        appBar: AppBar(
          title: const Text('Add Item'),
        ),
        body: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            children: [
              TextFormField(
                controller: _textEditingController,
                autofocus: true,
                decoration: InputDecoration(
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(10),
                  ),
                ),
              ),
              ElevatedButton(
                  onPressed: () {
                    shoppingList.add(ShoppingItem(
                        name: _textEditingController.text, done: false));
                    Navigator.pop(context);
                  },
                  child: const Text('Add')
                  ),
            ],
          ),
        ));
  }
}

2
  • 2
    Where is Configuration declared? Specifically: what is the type of Configuration.shoppingList? Commented Jul 18, 2022 at 19:34
  • Remove the const modifier from your shoppingList list declaration. Commented Jul 18, 2022 at 19:57

1 Answer 1

1

you can use something like this. creating an "Add" method inside the DataModel

void main(){

  NumberList list=NumberList([45,64,7]);
  
  list.numbers.forEach((e)=>print(e));///45,64,7
  list= list.add(5);//adds 5 in here it adds another Int and push into list variable itself
  list.numbers.forEach((e)=>print(e));///45,64,7,5
}
class NumberList {
  final List<int> _numbers;
 
  NumberList(this._numbers);
  NumberList add(int number) {
    return NumberList(_numbers.toList()..add(number));
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.