Comments (15)
listen
determines if the widget that use the value wants to rebuild when the value change.
For example, if:
Provider<int>(
value: 42,
child: ...
)
is later replaced by:
Provider<int>(
value: 24,
child: ...
Then this will trigger a rebuild of all widgets that called Provider.of(context)
with listen
to true
.
from provider.
Exactly
Which is also why provider love const constructors/statelesswidgets
from provider.
This should work.
Without const
, TestStatelessWidget's rebuild is triggered by State's setState even though listen = false
.
listen = false
just suppresses rebuild triggered by Provider not State's setState.
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Provider<int>.value(
value: _counter,
child: Column(
// add `const`
children: const <Widget>[TestStatelessWidget()],
))),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
class TestStatelessWidget extends StatelessWidget {
// add `const` constructor
const TestStatelessWidget();
@override
Widget build(BuildContext context) {
// result is updated anyway, listen = false does not work.
int result = Provider.of<int>(context, listen: false);
return Text("result: $result");
}
}
from provider.
It looks to me that you have two provides: one in the App and one in Screen A. The value you got in Screen B is from the App, which is a value literal of 123.
from provider.
@enetor Initially, Screen A is supposed to change the value
in Provider by doing setState({ value = 456 })
. :(
@enetor @rrousselGit Do you have recommendation on how to update the value
from Screen A?
from provider.
This should work.
TextField(
onChanged: (newText) {
Provider.of<Data>(context, listen: false).updateValue(newText);
},
)
from provider.
I believe that the new syntax (context.read
/watch
) makes it clear enough for this issue to be closed.
Feel free to correct me if you disagree
from provider.
Hi @rrousselGit, is it the way to update the value in a stateful widget?
For example:
Provider<int>(
value: _value,
child: ...
)
...
// Some widget calls (e.g. onClick event)
setState({
_value = 1234,
})
from provider.
Yes, that's how you should do it.
from provider.
@rrousselGit thank you for your confirmation! However, I'm stuck with this for quite long, not sure which part of my codes are wrong.
For example:
App init provider value as 123.
In Screen A, a button update the provider value to 456.
However, in screen B, it return the provider value as 123 instead of 456.
App:
MultiProvider(
providers: [
Provider<int>(
value: 123,
child: MaterialApp(
...
),
),
... // other providers
],
)
Screen A:
...
int _value = 0;
...
Widget build(BuildContext context) {
return Provider.of<int>(
value: _value,
child: Scaffold(
...
Button(
onPressed: () {
setState({
_value = 456, // Set value to 456
});
}
),
),
);
}
Screen B:
int _value;
Widget build(BuildContext context) {
final _provider = Provider.of<int>(context);
// when access _provider, the value still returns 123 instead of 456
}
from provider.
hi, what if we are using ChangedNotifierProvider? will it based on the notifiedListener, instead of value changed? and i suppose it will not rebuild if listen = false right?
from provider.
It seems listen parameter is not working in my example, whenever I call setState, it will just rebuild the child widget no matter listen is false or not. Am I doing something wrong? @rrousselGit
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Provider<int>.value(
value: _counter,
child: Column(
children: <Widget>[TestStatelessWidget()],
))),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
class TestStatelessWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
// result is updated anyway, listen = false does not work.
int result = Provider.of<int>(context, listen: false);
return Text("result: $result");
}
}
from provider.
Hey @rrousselGit and @mono0926,
I am having a similar issue to @PandaGeek1024 except that my app (in the following attached document) uses only StatelessWidgets so there is no call to setState anywhere.
For that reason, I am not sure what is causing my text widgets to rebuild themselves.
I have been trying to figure this out for 2 days now so any insight you could give would be much appreciated.
from provider.
@mono0926
Please can you explain why this works?
Thank you
from provider.
@Wes1324
I wrote about that in this article. This is written in Japanese, sorry.
https://link.medium.com/7a5FrRXFw0
from provider.
Related Issues (20)
- MaterialApp's builder function breaks provider. Home: does not HOT 5
- ε―δ»₯ζ·»ε δΈδΈͺδΈζζζ‘£εοΌ HOT 1
- Provider variable Null check operator used on a null value HOT 1
- Use `Selector` with Records instead of Tuple HOT 3
- Provider<ThemeProvider> not found for Consumer<ThemeProvider> works in debug; broken in release. HOT 8
- Create a DevTools extension for `package:provider` HOT 3
- Provider<DateTime Function()> it's a good practice ? HOT 1
- Flag to shorten error output HOT 4
- Copied comment from stackoverflow HOT 1
- Provider Extensions HOT 1
- Inherited_provider.dart class _CreateInheritedProviderState<T> throws error when I pass in 440 as double? works with 440.0 HOT 1
- Provider 6.1.0 appears to be missing devtools extension assets HOT 2
- context.read CAN be called within build method HOT 1
- Trying to solve code duplication when working with lists of Widgets in Consumer HOT 4
- The shouldUpdate Selector method returns the same previous and next values HOT 1
- Listen to Changes from a Notifier HOT 3
- DevTools failed to connect with package:provider HOT 3
- Stream issue with StreamProvider HOT 3
- Parameter createdByTestHelpers no longer used in withIgnored method HOT 1
- Mitigate ProviderNotFoundException with context redirection HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from provider.