The following assertion was thrown while calling onSubmitted for TextInputAction.done:
Tried to listen to a value exposed with provider, from outside of the widget tree.
This is likely caused by an event handler (like a button's onPressed) that called
Provider.of without passing `listen: false`. To fix, write:
Provider.of<Counter>(context, listen: false);
It is unsupported because may pointlessly rebuild the widget associated to the
event handler, when the widget tree doesn't care about the value.
The context used was: Boxes(dependencies: [_InheritedProviderScope<Counter?>], state:
_BoxesState#274b3)
'package:provider/src/provider.dart':
Failed assertion: line 274 pos 7: 'context.owner!.debugBuilding ||
listen == false ||
debugIsInInheritedProviderUpdate'
This error occurs after i added a line that inserts some data into a list.
TextField(
controller: _textController,
style: const TextStyle(color: Colors.white),
decoration:
const InputDecoration(
border:
OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(40.0)),
),
labelText: "Enter here",
),
onSubmitted: (value) {
setState(() {
Provider.of<Counter>(context, listen: false); // add listen: false here
_boxStorage += "- ${_textController.text}n";
_storage.insert(context.watch<Counter>().count, _boxStorage);
_saveSharedPreferences();
_textController.clear();
});
}
),
I already tried adding ‘Provider.of<Counter>(context, listen: false)’ which was the suggested solution in the error message. However, this solution does not work for me and the same error occurs once again after running the code. Could anyone help?
2
Answers
EDIT
Problem solved after changing "
context.watch<Counter>.count
" tocontext.read<Counter>.count
, and deletingProvider.of<Counter>(context, listen: false);
The requirement is not much clear from the code.
For line:
Not sure why you are adding this if you are not trying to access any value from provider. There is no need to add the line.