Skip to content

Instantly share code, notes, and snippets.

@duytq94
Last active March 24, 2020 11:54
Show Gist options
  • Select an option

  • Save duytq94/3baa05ce6c7441e89faa4793d92b6df7 to your computer and use it in GitHub Desktop.

Select an option

Save duytq94/3baa05ce6c7441e89faa4793d92b6df7 to your computer and use it in GitHub Desktop.
Future uploadFile() async {
String fileName = id;
StorageReference reference = FirebaseStorage.instance.ref().child(fileName);
StorageUploadTask uploadTask = reference.putFile(avatarImageFile);
StorageTaskSnapshot storageTaskSnapshot;
uploadTask.onComplete.then((value) {
if (value.error == null) {
storageTaskSnapshot = value;
storageTaskSnapshot.ref.getDownloadURL().then((downloadUrl) {
photoUrl = downloadUrl;
Firestore.instance
.collection('users')
.document(id)
.updateData({
'nickname': nickname,
'aboutMe': aboutMe,
'photoUrl': photoUrl
}).then((data) async {
await prefs.setString('photoUrl', photoUrl);
setState(() {
isLoading = false;
});
Fluttertoast.showToast(msg: "Upload success");
}).catchError((err) {
setState(() {
isLoading = false;
});
Fluttertoast.showToast(msg: err.toString());
});
}, onError: (err) {
setState(() {
isLoading = false;
});
Fluttertoast.showToast(msg: 'This file is not an image');
});
} else {
setState(() {
isLoading = false;
});
Fluttertoast.showToast(msg: 'This file is not an image');
}
}, onError: (err) {
setState(() {
isLoading = false;
});
Fluttertoast.showToast(msg: err.toString());
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment