flutter - 提供者在执行时不更新,Navigator.pop(context) 在 flutter 中

标签 flutter provider

这里我使用provider包进行状态管理。
我在 floatingActionButton 中有一个 SpeedDial 小部件。每当我使用 speedDial 在列表 mainGoalList 中添加内容并执行 Navigator.pop(context); 时,它确实会返回页面但不会更新列表。< br/>

快速拨号

        SpeedDialChild(
                  child: Icon(Icons.book),
                  backgroundColor: Colors.blue,
                  label: 'Add Notes',
                  labelStyle: TextStyle(
                    fontSize: 18.0,
                    color: Colors.black,
                  ),
                  onTap: () {
                    showModalBottomSheet(
                        context: context,
                        isScrollControlled: true,
                        builder: (context) => SingleChildScrollView(
                              child: Container(
                                padding: EdgeInsets.only(
                                    bottom: MediaQuery.of(context)
                                        .viewInsets
                                        .bottom),
                                child: AddNotes(),
                              ),
                            ));
                  }),
 

添加注释

          Center(
            child: FlatButton(
              onPressed: () {
                if (_actcontroller.text == null) {
                  print("Cannot add null topic");
                } else {
                  addingTheNotes();
                  Navigator.pop(context);
                }
              },
              child: Text("Add"),
              color: Colors.blue,
            ),
          )
 

添加注释

  addingTheNotes() {
    theDataProvider.ourAllnotes.add(
      TodaysNoteClass(
        note: _actcontroller.text,
        dateTime: theDataProvider.notesChoosenDate,
        status: false,
      ),
    );
    theDataProvider.showingTheTodaysList();
  }

这是更改通知器

  List<TodaysNoteClass> _ourAllnotes = [];
  List<TodaysNoteClass> get ourAllnotes => _ourAllnotes;
  set ourAllnotes(List<TodaysNoteClass> val) {
    _ourAllnotes = val;
    notifyListeners();
  }

消费类,这是我展示笔记的地方

class HomePage extends StatefulWidget {
  static const String id = 'homePage';
  final String todaysDate =
      DateFormat('d MMMM').format(DateTime.now()).toLowerCase();
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  var theDataProvider;

  Widget build(BuildContext context) {
    theDataProvider = Provider.of<TheData>(context, listen: false);

    return Consumer<TheData>(
      builder: (context, value, child) => SingleChildScrollView(
        child: Container(
          child: Column(
            children: [
              Container(
                height: 120,
                child: TodaysNote(),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

这是 TodaysNote 类

class TodaysNote extends StatefulWidget {
  TodaysNote({Key key}) : super(key: key);
  bool boxChecked = false;
  @override
  _TodaysNoteState createState() => _TodaysNoteState();
}

class _TodaysNoteState extends State<TodaysNote> {
  var theDataProvider;

  Widget build(BuildContext context) {
    theDataProvider = Provider.of<TheData>(context, listen: false);

    return Consumer<TheData>(
      builder: (context, value, child) => Container(
        child: theDataProvider.showingTheTodaysList(),
      ),
    );
  }
}

但是现在当我转到另一个屏幕然后回到上一个屏幕时。我看到了更新的列表。 我用提供者包装了主文件,用消费者包装了文件,但没有用。
这背后的原因可能是什么?

最佳答案

尝试使用这个:

Navigator.push( context, MaterialPageRoute( builder: (context) => SecondPage()), ).then((value) => setState(() {}));

关于flutter - 提供者在执行时不更新,Navigator.pop(context) 在 flutter 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64096672/

相关文章:

flutter - ListView.builder itemCount 未在 StreamBuilder 中更新

flutter - Dart 如何通过键过滤 map

flutter - 如何在 flutter 中绘制带有尖头/渐变线/的线?

c# - 什么是 ASP.NET 提供程序?

java - 关于编写 JNDI 服务提供程序的教程

flutter - 落在 pdf(EasyWebView) 顶部的窗口对话框 (showDatePicker) 的一部分不起作用,没有反应

flutter - 如何关闭特定的对话框

api - 如何在 flutter 中有效地使用 provider 获取初始数据

flutter - 如何在 Flutter 中处理堆栈或提供程序架构中的应用程序状态或生命周期

ASP.NET Active Directory 成员资格提供程序和 SQL 配置文件提供程序