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();
}
}
The code above is the provider class of mine and I want to use the list that I get from getStudentList list method to build widgets. The problem is it returns a future because I am getting the data from the database.
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(),
],
);
}),
This is where I am trying to build widgets. I am unable to do so.
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();
}
},
);
}
),
),
),
],
),
);
}
}