How to call setState() on a list from another dart file?

1,351 views
Skip to first unread message

barbaric...@gmail.com

unread,
Mar 5, 2018, 9:17:12 AM3/5/18
to Flutter Dev
I have an Alert dialog in file 1 and my list view in file 2 but I want to call setState() on list view from file 1, I've tried a bunch of stuff but had no luck so far. Can anyone help?

Ian Hickson

unread,
Mar 8, 2018, 4:37:10 PM3/8/18
to barbaric...@gmail.com, Flutter Dev
Usually the way to do this is to have a model object that represents the data, and have that object implement the Listenable interface (e.g. extend ChangeNotifier). Then you would have the dialog and the list view both be given that object and use that object to build themselves using AnimatedBuilder. The dialog would then talk to that object when the data is updated, and the object would call notifyListeners(), and that would cause the list to update.

On Mon, Mar 5, 2018 at 6:17 AM <barbaric...@gmail.com> wrote:
I have an Alert dialog in file 1 and my list view in file 2 but I want to call setState() on list view from file 1, I've tried a bunch of stuff but had no luck so far. Can anyone help?

--
You received this message because you are subscribed to the Google Groups "Flutter Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--

--
Ian Hickson

😸

Scott Stoll

unread,
Mar 13, 2018, 9:03:05 AM3/13/18
to Flutter Dev
Any chance we cold see a code example for this, with a few comments? I'm pretty new and even though I'm sure those instructions are pretty clear, I'm not familiar with a lot of the terms so it's a little hard to follow.

I'm also helping to put together a sort of cookbook for common stumbling blocks, such as this. If it's okay, I'd like to include the example code in it?

Thanks

Andrew Wilson

unread,
Mar 13, 2018, 12:50:58 PM3/13/18
to Scott Stoll, Flutter Dev
One way is to give the Widget that you want to call setState on a GlobalKey which you can then use to access its state but you *really* don't want to do that.

I recommend using a Scoped Model:


Dart package based off that (which admittedly has better documentation): https://pub.dartlang.org/packages/scoped_model





To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
If I am emailing outside office hours, this happens to be most convenient for my schedule on this day, I neither expect nor encourage a reply if the same is not true for you. 

Scott Stoll

unread,
Mar 13, 2018, 2:22:42 PM3/13/18
to Flutter Dev
Thanks Andrew, I'll look at that tonight.
Reply all
Reply to author
Forward
0 new messages