Add the ability to take a photo and save that image into Firebase Store

This commit is contained in:
Ricardo Markiewicz
2019-11-14 18:09:37 -03:00
parent bac9737bbb
commit f89104fffc
9 changed files with 171 additions and 46 deletions
+36 -4
View File
@@ -1,4 +1,9 @@
import 'dart:async';
import 'dart:io';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:uuid/uuid.dart';
class ExpensesRepository {
final String userId;
@@ -33,18 +38,45 @@ class ExpensesRepository {
.delete();
}
add(String categoryName, double value, DateTime date) {
Firestore.instance
add(String categoryName, double value, DateTime date, File selectedImage) async {
var document = Firestore.instance
.collection('users')
.document(userId)
.collection('expenses')
.document()
.setData({
.document();
await document.setData({
"category": categoryName,
"value": value,
"month": date.month,
"day": date.day,
"year": date.year,
});
if (selectedImage != null) {
var imageName = Uuid().v1();
var imagePath = "/users/$userId/$imageName.jpg";
final StorageReference storageReference = FirebaseStorage().ref().child(imagePath);
final StorageUploadTask uploadTask = storageReference.putFile(selectedImage);
final StreamSubscription<StorageTaskEvent> streamSubscription = uploadTask.events.listen((event) {
// You can use this to notify yourself or your user in any kind of way.
// For example: you could use the uploadTask.events stream in a StreamBuilder instead
// to show your user what the current status is. In that case, you would not need to cancel any
// subscription as StreamBuilder handles this automatically.
// Here, every StorageTaskEvent concerning the upload is printed to the logs.
print('EVENT ${event.type}');
});
// Cancel your subscription when done.
await uploadTask.onComplete;
streamSubscription.cancel();
document.setData({
"imageUrl": (await storageReference.getDownloadURL()).toString(),
}, merge: true,);
}
}
}