0

Iam working on a quiz project in flutter. I stored my quiz text files in folders according to the quiz category. And all these folders are stored in one root folder. So I should be able to list all the folders in this root folder ( which are categories ). And when category folder is opened the files in that corresponding folder should be listed.

The goal is to make buttons with each folder's name and the button will hold the path to open that folder.

ie, if the user clicks a button for folder, a list will be made containing the names of folders inside the clicked folder and according to this list a listview of buttons will be made.and finally when you open folder containing quiz files same will happen but button will contain the corresponding file path.

I have made the folder tree and functions to make buttons according to the names stored in list. But I have no idea where to put this folder inside the project.

I have tried puttin in the root of project, in Lib folder, in asset folder but flutter is failing to detect this directory.

I really need this to work for my project to go forward. Any help of pointers will be appreciated.

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • Put your folders and files at design time in the assets directory. Then they are packed with the apk and you can use them at run time. – blackapps Oct 05 '20 at 08:17

1 Answers1

0

For this to work you need multiple things:

  1. Viewing you folders and text files within flutter
  2. Listing your folders and text files
  3. Reading your text files

1 - Viewing you folders and text files within flutter

Since the assets notations doesn't support recursion, you have to either:

  • Write everything by hand
  • Use a plugin to generate the assets for you

Since you have many file I suppose the second option is the best for you. I did struggle to find one that worked but FlutterAssetSync is working like a charm according to my tests. The only constrain is that you have to put everything inside the "assets" folder (which is not really a constraint tbh).

Once you put you file in the assets folder, execute the plugin (under Tools > FlutterAssetSync). This will automatically add everything to your pubspec.yaml

2 - Listing your folders and text files

In order to get the name of your folders and your text files you can use the answers to this stackoverflow question. Basically here is the implementation:

Future<List> _getImages(String folderName) async {
  final manifestContent = await rootBundle.loadString('AssetManifest.json');
  final Map<String, dynamic> manifestMap = json.decode(manifestContent);

  return manifestMap.keys
      .where((String key) => key.contains(folderPath))
      .toList();
}

3 - Reading your text files

To read the content of a text file asset you must use loadString (as we did in _getImages btw). Here is a quick implementation:

Future<String> _getQuizzText(String quizzTextPath) async {
  return await rootBundle.loadString(quizzTextPath);
}

Putting everything together you should be able to make it work!

Lulupointu
  • 3,364
  • 1
  • 12
  • 28
  • Thank you for the answer brother. But I'm afraid this wouldn't solve my problem. Here I have a lot of folders containing the quiz files. If I'm using the assets folder, I have to manually register each folder in the 'pubsec.yaml' file. That is an inefficient way to do this task. – Ghanasyam KR Oct 05 '20 at 16:31
  • If you do as I said in step 1, this will register all you assets in one line – Lulupointu Oct 05 '20 at 17:13
  • I have tried that. But unfortunately the program is only detecting files in only the mentioned folders in the pubsec file. it's not recursively going in and detecting branch folders. I will have to mention each folder in the pubsec file. And yes it will register all the files in the mentioned folder I don't have to mention each file. But that's not the case of folders. Thank you :) – Ghanasyam KR Oct 05 '20 at 17:21
  • Oh... Right... I'll try to find something else then. – Lulupointu Oct 05 '20 at 17:25
  • That will be a great help. I have been searching for more than a week now and I couldn't find a solution for this. I am new to flutter so it's a bit challenging for me. Thank you @D.Lucas – Ghanasyam KR Oct 05 '20 at 17:31
  • @GhanasyamKR I finally got it working ! What a pain... I hope this works for you ;) – Lulupointu Oct 06 '20 at 14:08
  • Thanks a lot dude, hope this works. I will try this and let you know the results. Appreciate it Brother, means a lot to me. Thank you :) – Ghanasyam KR Oct 06 '20 at 18:46