To use ImagePicker() in Flutter to upload a list of multiple images to Firebase Storage, you can follow these steps:
import 'package:flutter/material.dart'; import 'package:imagepicker/imagepicker.dart'; import 'package:firebasestorage/firebasestorage.dart'; import 'dart:async'; import 'dart:io';
List<file> _imageList = [];
Future<void> _pickImages(ImageSource source) async { try { List<file> imageList = await ImagePicker.pickMultiImage(source: source); setState(() { _imageList.addAll(imageList); }); } catch (e) { print(e); } }
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; }
Pick Images from Device
GestureDetector( onTap: () async { await _pickImages(ImageSource.gallery); }, child: Text('Select 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.
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
Asked: 2023-05-19 21:00:21 +0000
Seen: 12 times
Last updated: May 19 '23
How can zooming similar to desktop be achieved in HTML webview browser using Flutter?
In what way can the appleID signin be utilized in flutter?
How to maintain state across different pages using flutter?
How can I change a CameraImage Stream into bytes or a file in Flutter?
How can the current page context be obtained by a Flutter GetX controller?