Multiupload Image Using Image List in flutter

4,589

Simple function to Upload Multiple Image Files.

import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';

  Future<Null> _uploadImages() async {
    _imageList.forEach((f) {
      final StorageReference _ref = storageImageRef.child(basename(f.path));
      final StorageUploadTask uploadTask = _ref.putFile(f);
    });
Share:
4,589
UnderdoX
Author by

UnderdoX

Updated on December 09, 2022

Comments

  • UnderdoX
    UnderdoX over 1 year

    I'm newbie. I try make app for upload some image using flutter. I have successfully pick some image from image picker, but when I try to write data to firebase storage, only one image was successfully uploaded. This is my code :

    List<File> _imageList = [];
    File _image;
    
      Future uploadFile() async {
        final FirebaseUser user = await FirebaseAuth.instance.currentUser();
        final userId = user.uid;
        final StorageReference firebaseStorageRef =
            FirebaseStorage.instance.ref().child(userId).child('images');
        final StorageUploadTask task = firebaseStorageRef.putFile(_image);
        return task;
      }
    

    And this my code for select and display image

     Future getImageFromGallery() async {
        var image = await ImagePicker.pickImage(source: ImageSource.gallery);
        setState(() {
          _image = image;
          _imageList.add(_image);
        });
      }
    
    List<Widget> builtImageDisplay() {
        return [
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: new Center(
              child: Padding(
                padding: const EdgeInsets.all(15.0),
                child: _imageList.length == 0
                    ? new Image.asset('assets/images/format.jpg')
                    : GridView.count(
                        shrinkWrap: true,
                        primary: false,
                        crossAxisCount: 4,
                        mainAxisSpacing: 5.0,
                        crossAxisSpacing: 5.0,
                        children: _imageList.map((File file) {
                          return GestureDetector(
                            onTap: () {},
                            child: new GridTile(
                              child: new Image.file(
                                file,
                                fit: BoxFit.cover,
                              ),
                            ),
                          );
                        }).toList(),
                      ),
              ),
            ),
          )
        ];
      }
    
  • UnderdoX
    UnderdoX over 5 years
    anmol.mahjhail Got It! Thanks.. U save my day! ^_^
  • griffins
    griffins over 4 years
    how can i get all download urls?