Describe the bug
As in the documentation, I tried to use the context.read<Counter> just to fetch a count value but throws an exception:
The following assertion was thrown building ProviderPractice(dirty):
Tried to usecontext.readinside either abuildmethod or theupdatecallback of a
provider.
This is unsafe to do so. Instead, consider usingcontext.watch.
If you usedcontext.readvoluntarily as a performance optimisation, the solution
is instead to usecontext.select.
'package:provider/src/provider.dart':
Failed assertion: line 584 pos 9: 'debugIsInInheritedProviderCreate ||
(!debugDoingBuild && !debugIsInInheritedProviderUpdate)'
The relevant error-causing widget was:
ProviderPractice
To Reproduce
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (_) => Counter(),
child: MaterialApp(
title: 'Custom Page Transitions',
home: ProviderPractice(),
theme: ThemeData.light().copyWith(
floatingActionButtonTheme: FloatingActionButtonThemeData(
backgroundColor: Colors.deepOrange,
),
),
),
);
}
}
class ProviderPractice extends StatelessWidget {
@override
Widget build(BuildContext context) {
print("I am rebuilding");
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () => context.read<Counter>().increment(),
child: Icon(Icons.add)),
body: Center(
child: Text(context.read<Counter>().count.toString(),
style: TextStyle(fontSize: 50.0))),
);
}
}
class Counter extends ChangeNotifier {
int count = 0;
void increment() {
count++;
notifyListeners();
}
}
Expected behavior
I was expected '0' to be shown on the screen even when I incremented the count variable.
Hey @chinkysight ,
Full Error states the following:
The following assertion was thrown building ProviderPractice(dirty):
Tried to use `context.read<Counter>` inside either a `build` method or the `update` callback of a provider.
This is unsafe to do so. Instead, consider using `context.watch<Counter>`.
If you used `context.read` voluntarily as a performance optimisation, the solution is instead to use `context.select`.
Now the reason you are getting this error is that you are using context.read<Counter> in StatelessWidget.
So; here as from the above error note, you can either use context.watch<Counter> or
context.select((Counter counter)=>counter.count) for performance optimization.
We can use context.read in StatefulWidget.
You can place it inside of initState.
Go to the documentation you will find examples of context.read with only StatefulWidget.
I hope, i was helpful here.
@vinodpahumalani why can't we use context.read() inside a StatelessWidget ?
@vinodpahumalani why can't we use context.read() inside a StatelessWidget ?
@chinkysight , From the Error note, it says that This is unsafe to do.
I don't know the exact reason right now, I'll try to find the reason.
but This is not a bug so, please remove the label bug.
I didn't assign the bug lable, it was auto labeled, I guess remi can only remove this lable.
@chinkysight , check out this link, contains the explanation for this.
@chinkysight , check out this link, contains the explanation for this.
That helped me, Thank You So Much @vinodpahumalani
Most helpful comment
That helped me, Thank You So Much @vinodpahumalani