首页 > 解决方案 > 提供程序在执行时未更新,Navigator.pop(context) 在颤动

问题描述

在这里,我使用提供程序包进行状态管理。
我在 floatingActionButton 中有一个 SpeedDial 小部件。每当我 mainGoalList使用 speedDial 在列表中添加某些内容时,Navigator.pop(context); 它确实会返回到页面但不会更新列表。

快速拨号

        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(),
      ),
    );
  }
}

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

标签: flutterprovider

解决方案


推荐阅读