Reputation: 768
I implemented listView to Display the data, which I save data into Sqflite Database
, fetched from database and display into listview. After saving data I pop
form fields page to listView page but my ListView is not updated, I need to move back and then open listView to see new Record.
List<SubjectModel> list;
int count = 0;
SubjectModel subjectModel;
@override
Widget build(BuildContext context) {
if (list == null) {
list = List<SubjectModel>();
updateListView();
}
return Scaffold(
appBar: AppBar(
title: Text('Subject'),
),
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.purple,
child: Icon(Icons.add),
onPressed: () {
Navigator.push(context,
new MaterialPageRoute<void>(builder: (context) {
return new CreateTask(SubjectModel('', '', '', '', '', '', ''),'Add Subject');
}));
}),
body: getSubListView()
);
}
ListView getSubListView(){
return ListView.builder(
itemCount: count,
itemBuilder: (BuildContext context, int position){
return Card(
color: Colors.white,
elevation: 2.0,
child: Column(
children: <Widget>[
ListTile(
title: Text(this.list[position].subject),
onLongPress: () => navigateToDetail(this.list[position],'Edit Subject'),
)
],
),
);
}
);
}
}
Upvotes: 1
Views: 19844
Reputation: 428
There has to be a setState() that is called, whenever you want to rebuild anything (your listView in this example).
I would add:
@override
void didChangeDependencies() {
super.didChangeDependencies();
setState(() {
updateListView();
});
}
Upvotes: 3