Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Flutter + Firebase storage on the web, work around
// Note this is just a workaround until the offical support of web
// We would need to add these as well
//// ignore_for_file: avoid_web_libraries_in_flutter
//import 'dart:html';
//import 'package:mime/mime.dart';
//import 'package:firebase/firebase.dart' as fb;
// Some upload button is pressed
onPressed: () async {
InputElement uploadInput = FileUploadInputElement();
uploadInput.click();
uploadInput.onChange.listen((e) async {
final files = uploadInput.files;
if (files.length == 1) {
final file = files[0];
final reader = new FileReader();
reader.onLoadEnd.listen((e) { });
reader.readAsDataUrl(file);
}
File file = files[0];
// Define destination at server
fb.StorageReference ref = fb.app('[DEFAULT]').storage().ref('company/logo');
fb.StorageReference logo = ref.child( file.name );
var metadata = UploadMetadata(contentType: lookupMimeType(file.name));
fb.UploadTask uploadTask = logo.put( file, metadata );
// Optional progress bar
ProgressDialog pr = ProgressDialog(context, type: ProgressDialogType.Normal, isDismissible: false);
pr.update( message: tr(context, 'please_wait') );
pr.show();
await uploadTask.future.then((UploadTaskSnapshot snapshot){
pr.update(message: tr(context, 'upload_completed'), progressWidget: Icon(Icons.check_circle_outline, color: Colors.green, size: 58,));
Future.delayed(Duration(seconds: 1)).then((value) {
pr.hide().whenComplete(() {
snapshot.ref.getDownloadURL().then((dynamic uri){
// uri is the link to the uploaded file
companyService.updateProfile( {'logo': uri.toString()}, (){} );
});
});
});
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment