how to send multiple files to firebase at once with flutter

Asked

Viewed 38 times

0

I need help with this question. I’m new to flutter. and can’t send more than 1 file at the same time to firebase with this code I can send 1 file without problems, but when selecting 2 files in the log shows the 2 files and in the firebase rises only 1 file with the name of the two files

import 'package:file_picker/file_picker.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class Estudos extends StatefulWidget {
  @override
  _EstudosState createState() => _EstudosState();
}

class _EstudosState extends State<Estudos> {
  final FirebaseStorage storage = FirebaseStorage.instance;
  String fileName;

  String fileName;
  List<PlatformFile> _paths;
  final FileType _pickingType = FileType.custom;
  final List<UploadTask> _tasks = <UploadTask>[];

Future<void> openFileExplorer() async {
    setState(() {});
    try {
      _paths = (await FilePicker.platform.pickFiles(
        type: _pickingType,
        allowMultiple: true,
        allowedExtensions: ['pdf', 'jpg'],
      ))
          ?.files;
    } on PlatformException catch (e) {
      //print("Unsupported operation" + e.toString());
    } catch (ex) {
      //print(ex);
    }
    if (!mounted) return;
    setState(() {
      fileName = _paths != null ? _paths.map((e) => e.name).toString() : '...';

      getFilesToUpload();
    });
  }

 final List<File> files = [];

  void getFilesToUpload() { 

   _paths.forEach((path) => files.add(File(path.path)));

    uploadFiles(files);
  }

  void uploadFiles(List<File> files) {
    files.forEach((file) => uploadFireStorage(file));
  }

  Future uploadFireStorage([File file]) async {
    final Reference pastaRaiz = storage.ref();
    final Reference archive = pastaRaiz.child('estudos').child(fileName);
    final UploadTask task = archive.putFile(file);
    setState(() {
      _tasks.add(task);
      task.snapshotEvents.listen((TaskSnapshot storageEvent) {
        if (storageEvent.state == TaskState.running) {
          setState(() {
            const CircularProgressIndicator();
          });
        } else if (storageEvent.state == TaskState.success) {
        }
      });
    });
  }```

1 answer

0

You can make your uploadFireStorage method receive a File and do the process, so you can call as many times as you want and the uploads can work at the same time.

Future uploadFireStorage(File file) async {
   
    final Reference pastaRaiz = storage.ref();
    final Reference arquivo = pastaRaiz.child('estudos').child(fileName);
    final UploadTask task = arquivo.putFile(file);
    setState(() {
      _tasks.add(task);
      task.snapshotEvents.listen((TaskSnapshot storageEvent) {
        if (storageEvent.state == TaskState.running) {
          setState(() {
            const CircularProgressIndicator();
          });
        } else if (storageEvent.state == TaskState.success) {}
      });
    });
  }

You need to generate a File list to move to the method that will upload.

void getFilesToUpload(){

  // Aqui recupero meus arquivos e gero o list
  List<File> files = [];
  paths.forEach((path) => list.add(File(path)));

  // passa suas lista de arquivos para fazer upload
  uploadFiles(files);

}

// Calls the method that will upload the file list simultaneously.

void uploadFiles(List<File>){

files.forEach((file) => uploadFireStorage(File file);

}
  • I did the closest to that and continues with the same problem, when sending 2 files rises only 1 to firebase. I will update the code, if you have any suggestions I would appreciate

Browser other questions tagged

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