简体   繁体   中英

how can I auto refresh listView in Flutter?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM