0

I'm still have a problem with the refresh of my screen after a function returns data.
This is my code:

return Scaffold(
  appBar: AppBar(
    title: Text(widget.title),
  ),
  body:
    FutureBuilder<List<ArchiveFile>>(
      future: _folderListFuture,
      builder: (context, snapshot) {
        print("!! Builder: ");
        print("ctxt: "+context.toString());
        print("snap: "+snapshot.toString());
        if (snapshot.hasError)
          return Text("ERROR ${snapshot.error}");
        if (snapshot.connectionState == ConnectionState.waiting) {
          // Otherwise, display a loading indicator.
          return Center(child: CircularProgressIndicator());
        }
        else if (snapshot.connectionState == ConnectionState.done) {
          access.disconnect();
          if (snapshot.hasData)
            return Center(
                child: ListView(
                  children: [
                    for (var folder in snapshot.data)
                      ListTile(
                        title: Text(folder.filename),
                        subtitle: Text(folder.type== ArchiveFileType.FileType_Folder? "Folder" : "File"),
                        onTap: () => onTapped(folder),
                        trailing: folder.type== ArchiveFileType.FileType_Folder ? Icon(Icons.chevron_right_outlined) : null,
                      )
                  ],
                ),
              );
          else return Text('No data');
        }
        else if (snapshot.connectionState == ConnectionState.none) {
          // Otherwise, display a loading indicator.
          return Center(child: CircularProgressIndicator());
        }
        else
          return Text(snapshot.connectionState.toString());
      }
      )

The _folderListFuture is defined as:

  _folderListFuture = access.folderContent();

The function is defined as:

Future<List<ArchiveFile>> folderContent() async {
print("[ftp] folderContent");

if (_connect==null)
  return null;

//return Future( () async {
  try {
    final List<FTPEntry> ftplist = await _connect.listDirectoryContent();

    // Creazione lista di Achievefiles
    final List<ArchiveFile> fileList = <ArchiveFile>[];
    ftplist.forEach((element) {
      print("- "+element.toString());
      ArchiveFile file = ArchiveFile(
          element.name,
          size: element.size,
          type: element.type==FTPEntryType.FILE ? ArchiveFileType.FileType_File : ArchiveFileType.FileType_Folder,
      );
      fileList.add(file);
    });
    print("BB ");
    _connect.disconnect().then( (ret){
      print("[foldercontent] disconnected: "+ret.toString());
    });
    print("BB "+fileList.toString());
    return fileList;
  }
  catch (e){
    print("[folderContent] e: "+e.toString());
  }
  print("CC");
  _connect.disconnect();
  return null;
//});
}

The debug window reports:

I/flutter (29031): Browse pressed
I/flutter (29031): !! Builder: 
I/flutter (29031): ctxt: FutureBuilder<List<ArchiveFile>>(dirty, state: _FutureBuilderState<List<ArchiveFile>>#4179e)
I/flutter (29031): snap: AsyncSnapshot<List<ArchiveFile>>(ConnectionState.none, null, null, null)
I/flutter (29031): [ftp-connect] server: 192.168.1.173 - user: user - pass: password
I/flutter (29031): [ftp-connect] Start connection ..
I/flutter (29031): [ftp-connect] done!
I/flutter (29031): [pageFolderesList] connect true
I/flutter (29031): [ftp] folderContent
I/flutter (29031): - name=app_flutterprova.txt;modify=2021-03-24 08:53:13.000;perm=null;type=file;size=6;unique=null;unix.group=null;unix.mode=null;unix.owner=null;unix.uid=-1;unix.gid=-1
I/flutter (29031): - name=F1234;modify=2021-03-21 20:20:30.000;perm=null;type=dir;size=0;unique=null;unix.group=null;unix.mode=null;unix.owner=null;unix.uid=-1;unix.gid=-1
I/flutter (29031): - name=Immagine2.png;modify=2021-03-13 18:18:17.000;perm=null;type=file;size=15920;unique=null;unix.group=null;unix.mode=null;unix.owner=null;unix.uid=-1;unix.gid=-1
I/flutter (29031): - name=Immagine3.png;modify=2021-03-13 18:31:29.000;perm=null;type=file;size=29750;unique=null;unix.group=null;unix.mode=null;unix.owner=null;unix.uid=-1;unix.gid=-1
I/flutter (29031): - name=prova.txt;modify=2021-03-24 08:43:26.000;perm=null;type=file;size=0;unique=null;unix.group=null;unix.mode=null;unix.owner=null;unix.uid=-1;unix.gid=-1
I/flutter (29031): BB 
I/flutter (29031): BB [Instance of 'ArchiveFile', Instance of 'ArchiveFile', Instance of 'ArchiveFile', Instance of 'ArchiveFile', Instance of 'ArchiveFile']
I/flutter (29031): [foldercontent] disconnected: true

The FutureBuilder is fired the first time with ConnectionState.none value.
Then the communication started and it received data.
When the data is returned the FutureBuilder is not fired, as I expected, and the CircularProgressIndicator is on the screen. If I force the refresh of the screen I see the data on the screen...

Where is the fault, please?

SteMMo
  • 392
  • 1
  • 4
  • 23
  • I'm not sure about this but did you use set state in order for the state to be changed when data is received? – adrsh23 Mar 24 '21 at 11:48
  • Where? in the folderContent() function? It resides in different class .. – SteMMo Mar 24 '21 at 12:00
  • The part where you're checking whether data is there or not because you said even after data is received its still showing circular progress indicator. Also you said after hot reload data is then been shown. Try using bool variable in init State which checks data is received or not and keep in set state – adrsh23 Mar 24 '21 at 14:01
  • Is it possible that the page refresh is not fired because the first time the FutureBuiler is called the _folderListFuture variable is null, not yet initialized? – SteMMo Mar 30 '21 at 15:25
  • [Solved] Confirm that the _folderListFuture was set too late and the FutureBuilder saw the value as null; then it was not able to fire when the variable was set and finished. – SteMMo Mar 30 '21 at 17:01

1 Answers1

0

[Solved]

I can confirm that the _folderListFuture was set too late and the FutureBuilder saw the value as null; then it does nothing and it was not able to fire when the variable was set and finished.

SteMMo
  • 392
  • 1
  • 4
  • 23