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')
),
],
),
));
}
}
Configurationdeclared? Specifically: what is the type ofConfiguration.shoppingList?constmodifier from yourshoppingListlist declaration.