Created
January 8, 2020 09:48
-
-
Save ialhashim/d0c867daa2dc02cf4e178374abd0ca49 to your computer and use it in GitHub Desktop.
Flutter + Firebase storage on the web, work around
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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