Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get a list of files from the directory and pass it to the ListView?

Tags:

flutter

dart

I get the list of files from the user's folder. The names of the files I transfer to the ListView.builder. It's work, but I think, this is bad architecture.

A method _getFilesFromDir() call with a high frequency.

How to make the correct list generation, so as not to update the interface without changing the file list?

class CharacteristList extends StatefulWidget {
  @override
  _CharacteristListState createState() => new _CharacteristListState();
}    
class _CharacteristListState extends State<CharacteristList> {
  List<String> filesList = new List<String>();
  List<String> filesL = new List<String>();
  @override    
  void initState() {
    super.initState();
    filesList = [];
  }    
  Future<List<String>> _getFilesFromDir() async{
    filesL = await FilesInDirectory().getFilesFromDir();
    setState(() {
      filesList = filesL;
    });
    return filesList;
  }    
  _getFilesCount(){
    _getFilesFromDir();
    int count = filesList.length;
    return count;
  }    
  @override
  Widget build(BuildContext context) {    
    return new Scaffold(
      appBar: new AppBar(
        title: const Text('Список документов'),
      ),
      body: new Column(
        children: <Widget>[
          new Expanded(
              child: new ListView.builder(
                //TODO не успевает сформировать список файлов
                itemCount: _getFilesCount(),
                itemBuilder: (context, index){
                  return new CharacteristListItem(filesList[index]);
                },
              ),
          ),
        ],
      ),
      floatingActionButton: new FloatingActionButton(
        onPressed: () {
          Navigator.push(context,
              new MaterialPageRoute(builder: (context)
          => new StartScreen()),
          );},
        child: new Icon(Icons.add),
      ),
    );
  }    
}
like image 268
Вячеслав Avatar asked May 25 '18 03:05

Вячеслав


People also ask

How do I list files in a directory in flutter?

To list all the files or folders, you have to use flutter_file_manager, path, and path_provider_ex flutter package. Add the following lines in your pubspec. yaml file to add this package in your dependency. Add read / write permissions in your android/app/src/main/AndroidManifest.

How do I get a list of files from File Explorer?

Select all the files, press and hold the shift key, then right-click and select Copy as path. This copies the list of file names to the clipboard. Paste the results into any document such as a txt or doc file & print that.


2 Answers

  // add dependancy in pubspec.yaml
 path_provider:

 import 'dart:io' as io;
    import 'package:path_provider/path_provider.dart';

    //Declare Globaly
      String directory;
      List file = new List();
      @override
      void initState() {
        // TODO: implement initState
        super.initState();
        _listofFiles();
      }

    // Make New Function
    void _listofFiles() async {
        directory = (await getApplicationDocumentsDirectory()).path;
        setState(() {
          file = io.Directory("$directory/resume/").listSync();  //use your folder name insted of resume.
        });
      }

    // Build Part
    @override
      Widget build(BuildContext context) {
        return MaterialApp(
          navigatorKey: navigatorKey,
          title: 'List of Files',
          home: Scaffold(
            appBar: AppBar(
              title: Text("Get List of Files with whole Path"),
            ),
            body: Container(
              child: Column(
                children: <Widget>[
                  // your Content if there
                  Expanded(
                    child: ListView.builder(
                        itemCount: file.length,
                        itemBuilder: (BuildContext context, int index) {
                          return Text(file[index].toString());
                        }),
                  )
                ],
              ),
            ),
          ),
        );
      }
like image 139
Jay Gadariya Avatar answered Oct 12 '22 09:10

Jay Gadariya


Don't call _getFilesCount() in build(). build() can be called very frequently. Call it in initState() and store the result instead of re-reading over and over again.

like image 20
Günter Zöchbauer Avatar answered Oct 12 '22 10:10

Günter Zöchbauer