ListView not rebuilding when returned to screen even after using setState()?

963 views
Skip to first unread message

Faraz Khan

unread,
Jul 23, 2020, 12:29:37 PM7/23/20
to Flutter Development (flutter-dev)
Hi,

I have a list that is populated based on number of files in a folder. On application start it manages to build my list correctly and as expected.

But on the new note screen the user has the option to save a note in this folder. When I come back to this screen the list does not rebuild based on this new file added. When I close application and restart, the file shows up in my list.


I used .then() with Navigator.push and rebuilt my list in setState() but it still does not update.

What am I doing wrong? Below is my code:

class _HomeListViewState extends State<HomeListView> {
  Directory easyDir;
  var txtList;

  @override
  void initState(){
    super.initState();
    easyDir = Directory(widget.folderPath);
    buildTxtList();
  }

  void buildTxtList(){
    //list of paths of my txt files in the folder
    txtList = easyDir
        .listSync()
        .map((item) => item.path)
        .where((item) => item.endsWith(".txt"))
        .toList();
  }

  @override
  Widget build(BuildContext context) {

    print(txtList.length);
    
    return ListView.builder(
      itemCount: txtList.length,
      padding: EdgeInsets.all(10.0),
      itemBuilder: (context, index){
        File file = new File(txtList[index]);
        String name = file.path.split('/').last;
        return Card(
          color: (index % 2 == 0) ? Colors.grey.shade300 : Colors.white,
          shadowColor: Colors.teal,
          elevation: 7.0,
          child: ListTile(
            leading: Icon(Icons.assignment),
            title: Text(name),
            trailing: Icon(Icons.keyboard_arrow_right),
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(builder: (context) => NoteEditScreen(
                  txtPath: file.path,
                ),
                ),
              ).then((value) {
                setState(() {
                  buildTxtList();
                });
              });
            },
            onLongPress: () => print('list tile long pressed'),
          ),
        );
      },
    );
  }
}

Suzuki Tomohiro

unread,
Jul 23, 2020, 12:40:20 PM7/23/20
to Faraz Khan, Flutter Development (flutter-dev)
With debugger can you check the timing of

- new file creation in the directory
- list rebuild after Navigation.push

You expect the two happen in this order. But the actual behavior you observe is the opposite.

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/520840ee-fa77-425b-ab52-d6b1ce9663b1o%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages