GithubHelp home page GithubHelp logo

Doubt with Provider.of about provider HOT 15 CLOSED

rrousselgit avatar rrousselgit commented on May 19, 2024 2
Doubt with Provider.of

from provider.

Comments (15)

rrousselGit avatar rrousselGit commented on May 19, 2024 5

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.

rrousselGit avatar rrousselGit commented on May 19, 2024 3

Exactly

Which is also why provider love const constructors/statelesswidgets

from provider.

mono0926 avatar mono0926 commented on May 19, 2024 2

@PandaGeek1024

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.

enetor avatar enetor commented on May 19, 2024 1

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.

wheel1992 avatar wheel1992 commented on May 19, 2024 1

@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.

mono0926 avatar mono0926 commented on May 19, 2024 1

@Wes1324

This should work.

         TextField(
            onChanged: (newText) {
              Provider.of<Data>(context, listen: false).updateValue(newText);
            },
          )

from provider.

rrousselGit avatar rrousselGit commented on May 19, 2024 1

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.

wheel1992 avatar wheel1992 commented on May 19, 2024

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.

rrousselGit avatar rrousselGit commented on May 19, 2024

Yes, that's how you should do it.

from provider.

wheel1992 avatar wheel1992 commented on May 19, 2024

@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.

jasonlaw avatar jasonlaw commented on May 19, 2024

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.

PandaGeek1024 avatar PandaGeek1024 commented on May 19, 2024

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.

Wes1324 avatar Wes1324 commented on May 19, 2024

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.

Listen doesn't work.txt

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.

Wes1324 avatar Wes1324 commented on May 19, 2024

@mono0926
Please can you explain why this works?
Thank you

from provider.

mono0926 avatar mono0926 commented on May 19, 2024

@Wes1324
I wrote about that in this article. This is written in Japanese, sorry.
https://link.medium.com/7a5FrRXFw0

from provider.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.