Flutter Firebase:如何上传多个图像?

问题描述 投票:0回答:1

我有这个按钮来选择图像:

IconButton(
            onPressed: () async {
              final results = await FilePicker.platform.pickFiles(
                allowMultiple: true,
                type: FileType.custom,
                allowedExtensions: ['png', 'jpg'],
              );
              if (results == null) {
                ScaffoldMessenger.of(context).showSnackBar(
                const SnackBar(
                    content: Text("No Image selected")
                ),
                );
                return null;
              }

              final path = results.files.single.path!;
              final fileName = results.files.single.name;
              final docID = widget.docID;

                storage
              .uploadFile(path, fileName, docID)
              .then((value) => print('Done'));
            },

          icon: Icon(),
        ),

...然后是上传功能:

Future<void> uploadFile(

String filePath,
String fileName,
String docID,
) async {
  File file = File(filePath);
  try {

    await storage.ref('images/test/$docID/$fileName').putFile(file);
  } on firebase_core.FirebaseException catch (e) {
    print(e);
  }
}

这适用于单个图像,现在我想选择并上传多个文件。选择已经可以了,但是我如何上传它们?

我尝试将文件另存为列表,但不知道如何继续: 列出文件 = results.paths.map((path) => File(path!)).toList();

flutter firebase dart filepicker
1个回答
0
投票
    IconButton(
                onPressed: () async {
                  final results = await FilePicker.platform.pickFiles(
                    allowMultiple: true,
                    type: FileType.custom,
                    allowedExtensions: ['png', 'jpg'],
                  );
                  if (results == null) {
                    ScaffoldMessenger.of(context).showSnackBar(
                    const SnackBar(
                        content: Text("No Image selected")
                    ),
                    );
                    return null;
                  }
                  final List<String> filePaths = results.paths!;
                  for (String path in filePaths) {
                  final fileName = path.split('/').last;
                  final docID = widget.docID;

                 await storage.uploadFile(path, fileName, docID);
    }
  },
    
              icon: Icon(),
            ),
© www.soinside.com 2019 - 2024. All rights reserved.