如何使用数据库中未来的对象列表来触发使用 flutter 提供程序的重建?

How can I use a future list of object from database to trigger rebuild using flutter provider?

class StudentProvider extends ChangeNotifier {
DataBaseHelper data = DataBaseHelper();
List<StudentModel>? student = [];
List<StudentModel>? searchStudentList;
Future<List<StudentModel>> getStudentList() async {
student = await data.getStudent();
return student!;
}

void insert(StudentModel studentModel) async {
print("reached insert in provider class");
await data.insertStudent(studentModel);
notifyListeners();
}
}

上面的代码是我的提供程序 class,我想使用从 getStudentList 列表方法获得的列表来构建小部件。问题是它 returns 一个未来,因为我正在从数据库中获取数据。

  body: TabBarView(children: [
          Consumer<StudentProvider>(
              builder: (context, StudentProvider student, ch) {
            print("rebuild happened");
            return ListView(
              children: [
                ...student.getStudentList().map((e) {
                  return HomeScreen(student: e);
                }).toList(),
              ],
            );
          }),

这是我尝试构建小部件的地方。我做不到。

in getStudentList() you have used  Future<List<StudentModel>> so that future can not access in listview.

> So please use streambuilder or remove the future from the list



 class StreamBuilderUsage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              Expanded(
                child: Center(
                  child: Consumer<StudentProvider>(
                      builder: (context, StudentProvider student, ch) {
                      return StreamBuilder(
                        stream: student.getStudentList().asStream(),
                        builder: (context, snapshot){
                          print(snapshot.connectionState);
                          if(snapshot.hasData){
                            var data = snapshot.data as List<String>;
                            return ListView.builder(
                              itemCount: data.length,
                              itemBuilder: (context, index){
                                return Text(data[index]);
                              },
                            );
                          }else{
                            return CircularProgressIndicator();
                          }
                        },
                      );
                    }
                  ),
                ),
              ),
            ],
          ),
        );
      }
    }

我接受的答案也有效,我们可以使用 Future builder 来实现同样的效果。 这是代码:-

 body: TabBarView(
          children: [
            Consumer<StudentProvider>(
              builder: (context, StudentProvider student, ch) {
                print("rebuild happened");
                return FutureBuilder(
                  future: student.getStudentList(),
                  builder: (context, snapshot) {
                    if (snapshot.hasData) {
                      List<StudentModel> data =
                          snapshot.data as List<StudentModel>;
                      return ListView.builder(
                          itemCount: data.length,
                          itemBuilder: (context, index) {
                            return HomeScreen(student: data[index]);
                          });
                    } else
                      return Container(child: Text("No data"));
                  },
                );
              },
            ),
            Container(),//neglect this container 
          ],
        ),