最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

flutter - How Can I Force a Parent View to Update Itself? - Stack Overflow

programmeradmin3浏览0评论

I've got a small Flutter application with a structure like this:

HomePage
  PageView
    Tab1 - list of data w/ FutureBuilder
    Tab2 - list of data w/ StreamBuilder
    Tab3 - list of data w/ StreamBuilder
  Floating Action Button

The FAB does a different thing based on which tab is currently active (i.e. on Tab1 it opens an "Add New Thing" form, but on Tab2 it opens an "Add New Category of Thing" form). When I tap the FAB from Tab1 and then complete & submit the "Add New Thing" form, I am returned to the Tab1 view - but the data isn't being refreshed. (The future is defined in a variable outside the build method specifically so it isn't called every time the view is loaded - but I need to be able to load it on-demand occasionally.)

The FAB/submit/return to view process works as expected on Tab2 and Tab3, I suspect because they are streams - but I can't use a stream on Tab1. So how can I force Tab1 to refresh the future it's using when I submit the form & pop the view?

Here's the FAB code & the form code snippets...

/// FloatingActionButton code snippet
onPressed: () async {
          switch (selectedPage) {
            case 0:
              debugPrint('add a new transaction');
              await Navigator.of(context).push(MaterialPageRoute(
                  fullscreenDialog: true,
                  builder: (context) => AddTransactionTest()));
              setState(() {});
... other cases for Tab2, Tab3 etc

/// Form code snippet
onPressed: () async {
                    try {
                      // do some stuff
                      await insertTransaction(txn);
                      if (mounted) {
                        ScaffoldMessenger.of(context).showSnackBar(
                            SnackBar(content: Text('Transaction saved!')));
                        Navigator.pop(context);
                      }
... error-handling stuff
发布评论

评论列表(0)

  1. 暂无评论