Is it possible to list the name of the Assets in a Flutter app?

Asked

Viewed 410 times

3

I need to know which ones Assets I uploaded it to an , to reflect and make a decision.

I know I can list which ones Assets that I wish to climb into the app through the pubspec.yaml:

...

flutter:
  assets:
     - assets/image.png
     - scripts/

...

Documentation

In the above case, I’m saying I want to deliver the file on the way assets/image.png and also all files within the directory scripts/.

I can carry one Asset through the AssetBundle (like the rootBundle, already created a priori in Flutter).

For example, I can load the Asset scripts/V1.0__baseline.sql thus:

...

// import 'dart:async' show Future;
// import 'package:flutter/services.dart' show rootBundle;

Future<String> loadAsset() async {
  return await rootBundle.loadString('scripts/V1.0__baseline.sql');
}

Documentation

However, I couldn’t find anything in the documentation that would allow me to list the files inside the folder scripts to then be able to do something with them.


If that’s not possible, I’ll accept workarounds, how to generate a file with the folder listing scripts and add it as a Asset (solution similar to what I did to solve a analogous problem in Totalcross) through some configuration in the pubspec.yaml with any possible script call (bash/PowerShell/cmd/dart).

  • Didn’t the answer help you? What was missing?

  • @Juliohenriquebitencourt timely to test, since I achieved a workaround that widely makes this difficulty "overcome" (in a silly way and not flutter to be). So until the delivery is complete I don’t think I can test

1 answer

2

It is not necessary to generate a new file with the names of the others assets then carry it via AssetBundle, That’s because Flutter himself already does it :)

When building your application, realize that a file is generated AssetManifest.json with registration of all app Assets:

inserir a descrição da imagem aqui

So taking into consideration an app with 3 name images image_1, image_2 and image_3 inside assets/images:

assets:
  - assets/images/

The content of the json file is as follows:

{  
   "packages/cupertino_icons/assets/CupertinoIcons.ttf":[  
      "packages/cupertino_icons/assets/CupertinoIcons.ttf"
   ],
   "assets/images/image_1.png":[  
      "assets/images/image_1.png"
   ],
   "assets/images/image_2.png":[  
      "assets/images/image_2.png"
   ],
   "assets/images/image_3.png":[  
      "assets/images/image_3.png"
   ]
}

With this then just read this file as you already know, and extract the necessary information, an example below:

Future<List<String>> _findFiles(BuildContext context) async {
  var manifest =
      await DefaultAssetBundle.of(context).loadString('AssetManifest.json');

  Map map = json.decode(manifest);
  List<String> values =
      map.keys.where((k) => k.contains('assets/image')).toList();
  return Future.value(values);
}

Above, read the file with DefaultAssetBundle, made the json code with dart:convert for a Map, and extracted the necessary Keys.

Full example:

import 'dart:convert';
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: SafeArea(
        child: Scaffold(
          appBar: AppBar(
            title: Text('Nome dos Assets'),
          ),
          body: FutureBuilder(
            future: _findFiles(context),
            builder:
                (BuildContext context, AsyncSnapshot<List<String>> snapshot) {
              if (snapshot.hasData) {
                final names = snapshot.data;

                return ListView.builder(
                  itemCount: names.length,
                  itemBuilder: (BuildContext context, int index) {
                    final asset = names[index];

                    return ListTile(title: Text(asset));
                  },
                );
              }

              return Center(
                child: Text('Carregando'),
              );
            },
          ),
        ),
      ),
    );
  }
}

The result:

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.