Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To use ImagePicker() in Flutter to upload a list of multiple images to Firebase Storage, you can follow these steps:

  1. Import the relevant libraries:

import 'package:flutter/material.dart'; import 'package:imagepicker/imagepicker.dart'; import 'package:firebasestorage/firebasestorage.dart'; import 'dart:async'; import 'dart:io';

  1. Create a list to store the selected images:

List<file> _imageList = [];

  1. Create a function to handle selecting images from the device:

Future<void> _pickImages(ImageSource source) async { try { List<file> imageList = await ImagePicker.pickMultiImage(source: source); setState(() { _imageList.addAll(imageList); }); } catch (e) { print(e); } }

  1. Create a function to upload the selected images to Firebase Storage:

Future<list<string>> _uploadImages() async { List<string> downloadUrls = []; try { for (File image in _imageList) { String fileName = DateTime.now().millisecondsSinceEpoch.toString(); StorageReference reference = FirebaseStorage.instance.ref().child(fileName); StorageUploadTask uploadTask = reference.putFile(image); await uploadTask.onComplete; String downloadUrl = await reference.getDownloadURL(); downloadUrls.add(downloadUrl); } } catch (e) { print(e); } return downloadUrls; }

  1. Call the _pickImages() function when you want to select images from the device:

Pick Images from Device

GestureDetector( onTap: () async { await _pickImages(ImageSource.gallery); }, child: Text('Select Images'), ),

  1. Call the _uploadImages() function when you want to upload the selected images:

Upload Images to Firebase Storage

RaisedButton( onPressed: () async { List<string> downloadUrls = await _uploadImages(); // Do something with downloadUrls }, child: Text('Upload Images'), ),

Note: The above code is just an example, you may need to adjust it for your specific use case.