Created
August 24, 2018 15:08
-
-
Save vinceramcesoliveros/e0e5357524f2db39e36f899f2104fcd7 to your computer and use it in GitHub Desktop.
Upload Image to Firebase Storage
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
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