Flutter web file image picker and upload firebase storage

 Flutter web file image picker and upload Firebase storage

In this post i am showing full coding of flutter web image picker to upload data in storage fully working code .


import 'dart:html';
 
 selectImageFile(context) async {
try {
InputElement input = FileUploadInputElement()..accept = 'image/*';
input.click();
setState(() {
isLoading = true;
});
input.onChange.listen((event) async {
var file = input.files.first;
var reader = FileReader();
reader.readAsDataUrl(file);
reader.onLoadEnd.listen((event1) async {
print('loading end');
var ref = storage.ref('complaints').child(basename(file.name));
firebase_storage.SettableMetadata metadata =
firebase_storage.SettableMetadata(
customMetadata: <String, String>{"uid": user.uid},
);
firebase_storage.UploadTask uploadTask = ref.putBlob(file, metadata);
uploadTask.asStream().listen((event) {
setState(() {
isLoading = true;
progress = event.bytesTransferred.toDouble() /
event.totalBytes.toDouble();
print(progress);
});
}).onError((error) => {
ScaffoldMessenger.of(context).showSnackBar(new SnackBar(
content: new Text(error.toString()),
backgroundColor: Colors.lightGreen,
)),
});

uploadTask.then((res) async {
var url = await res.ref.getDownloadURL();

});
});
});
} catch (e) {
print(e);
}
}
Previous
Next Post »