Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vinceramcesoliveros/e0e5357524f2db39e36f899f2104fcd7 to your computer and use it in GitHub Desktop.
Save vinceramcesoliveros/e0e5357524f2db39e36f899f2104fcd7 to your computer and use it in GitHub Desktop.
Upload Image to Firebase Storage
Future<Null> uploadFile(String filepath) async {
final String fileName = '${DateTime.now().millisecondsSinceEpoch}.jpg';
final ByteData bytes = await rootBundle.load(filepath);
final Directory tempDir = Directory.systemTemp;
final File file = File('${tempDir.path}/$fileName');
file.writeAsBytes(bytes.buffer.asInt8List(), mode: FileMode.write);
final StorageReference storageRef =
FirebaseStorage.instance.ref().child('eventImages/$fileName');
final StorageUploadTask task = storageRef.putFile(file);
final Uri downloadUrl = (await task.future).downloadUrl;
path = downloadUrl.toString();
}
// Think of it that the button works.
floatingActionButton: FloatingActionButton.extended(
backgroundColor: Colors.red[300],
icon: Icon(Icons.create),
label: Text("Create Event"),
onPressed: () async {
await uploadFile(_image.path);
});
},
),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment