flutter multiple image upload into firestore

How to upload multiple images into firestore flutter

I am today teach you how to upload multiple images into the flutter cloud firestore see the code below
How to upload multiple images into firestore flutter

How to upload multiple images into firestore flutter




import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/services.dart';
import 'package:gadgetsteach/services/database_service.dart';
import 'package:multi_image_picker/multi_image_picker.dart';

Map<String, dynamic> model = new Map<String, dynamic>();

DatabaseService db = new DatabaseService();

var send = [];
int i = 0;
class MultiImage {

getImageList(id) async {

send = [];
var resultList = await MultiImagePicker.pickImages(
maxImages: 10,
enableCamera: true,
);

// The data selected here comes back in the list

print(resultList);

for (var imageFile in resultList) {

postImage(imageFile, id).then((downloadUrl) {

// Get the download URL

print(downloadUrl.toString());

}).catchError((err) {

print(err);
});
}
}

Future postImage(Asset imageFile, id) async {
ByteData byteData = await imageFile.requestOriginal();
List<int> imageData = byteData.buffer.asUint8List();
String fileName = DateTime.now().millisecondsSinceEpoch.toString();
StorageReference reference =
FirebaseStorage.instance.ref().child('gadgetsteach/$fileName');
StorageUploadTask uploadTask = reference.putData(imageData);

// StorageTaskSnapshot storageTaskSnapshot = await uploadTask.onComplete;

// return storageTaskSnapshot.ref.getDownloadURL();
await uploadTask.onComplete
..ref.getDownloadURL().then((url) {
model['images' + '$i'] = url;
db.addDetail(model, id);
return i++;
});
}
}


and my service part we do this code place both code into your project flutter app:-


Future<void> addDetail(dynamic model, id) {
model["createdAt"] = FieldValue.serverTimestamp();
return _db.collection('images').document(id).updateData(model);
}

After that, they work fine thanks.
comment for any problem into the code
Previous
Next Post »