Ask Your Question
1

What is the way to use ImagePicker() in flutter to upload a list of multiple images to Fire storage?

asked 2023-05-19 21:00:21 +0000

ladyg gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-05-19 21:30:02 +0000

devzero gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-19 21:00:21 +0000

Seen: 12 times

Last updated: May 19 '23