首页 > 解决方案 > How to get data from database after event from another screen

问题描述

I came from Android Development, so, I'll try to explain my situation in Android terms in some cases. So, I have the MainScreen, which uses Scaffold and has FAB. The body of this screen is another widget (as Fragment in Android). When I click on the FAB (which is on MainScreen), bottom modal is opened. By this model, I can add data to the database. But I also want to add new data to my widget (fragment) at the same time. And I don't know in which method of lifecycle I should do call to DB. In android such method is onResume, but I didn't find it's analogue in Flutter.

I'll appreciate any help, thanks in advance!

UPD

There's my screen:

enter image description here

Bottom navigation and FAB is on the MainScreen. ListView is on the widget, which is body of MainScreen's Scaffold. Another my step is clicking on FAB. This click opens bottom modal

enter image description here

When I click save on the modal, data is saved to DB and modal close. And after this closing I want to see new database entry, which I just added from the modal. Now I can see new notes only after closing and then opening screen again. Here's my code:

class NotesScreen extends StatefulWidget {
  @override
  _NotesScreenState createState() => _NotesScreenState();
}

class _NotesScreenState extends State<NotesScreen> with WidgetsBindingObserver {
  List<Note> _notes = new List<Note>();


  @override
  void initState() {
    super.initState();
    Fimber.i("Init");
    WidgetsBinding.instance.addObserver(this);
    NotesDataManager().getNotes().then((value) {
      _notes = value;
      setState(() {
      });
    });
  }


  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    switch (state){
      case AppLifecycleState.resumed:
        NotesDataManager().getNotes().then((value){
          _notes = value;
          setState(() {
          });
        });
        Fimber.i("Resumed");
        break;
      case AppLifecycleState.inactive:
        Fimber.i("Inactive");

        break;
      case AppLifecycleState.paused:
        Fimber.i("Paused");
        break;
      case AppLifecycleState.suspending:
        Fimber.i("Suspending");
        break;
    }
    super.didChangeAppLifecycleState(state);
  }

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      backgroundColor: Color(AppColors.layoutBackgroundColor),
      body: Container(
        child: ListView.builder(
          itemCount: _notes.length,
          itemBuilder: (BuildContext context, int index) {
            return NotesListItemWidget(note: _notes[index]);
          },
        ),
      ),
    );
  }
}

As you can see, I added WidgetsBindingObserver, but it didn't help

标签: flutter

解决方案


您可能希望在您的应用程序中使用状态管理。最简单的是提供者。您的模型应该扩展ChangeNotifier,并且每当您添加模型的新实例时,都会通知应用程序:notifyListener. 在build您将实施ChangeNotifierProvider. 这里有一些教程:

提供者示例 1

提供者示例 2

或者你可以只搜索Provider


推荐阅读