Reputation: 143
I tried to store image to firebase storage and try to get an image URL to the database these are the codes. this is the code that I used to upload the image to firebase storage.
uploadNotice()async{
String fileName=basename(noticepic.path);
final StorageReference firebaseStorageRef=FirebaseStorage.instance
.ref().child('cisnotices/$fileName');
StorageUploadTask task = firebaseStorageRef.putFile(noticepic);
var dowurl = await (await task.onComplete).ref.getDownloadURL();
var url=dowurl.toString();
noticeProvider.changePhotoUrl(url).then((val){
print('photo url got');
}).catchError((e){
print(e);
});
}
NoticeProvider noticeProvider=NoticeProvider();
here is the noticeprovider.dart
I used to change the image URL
class NoticeProvider with ChangeNotifier {
final firestoreService=FirestoreService();
String _noticeId;
String _userName;
String _userPost;
String _date;
String _photoUrl;
var uuid=Uuid();
//getters
String get userName => _userName;
String get userPost => _userPost;
String get date => _date;
String get photoUrl => _photoUrl;
//setters
changeUserName(String value){
_userName=value;
notifyListeners();
}
changeUserPost(String value){
_userPost=value;
notifyListeners();
}
changeDate(String value){
_date=value;
notifyListeners();
}
changePhotoUrl(String value){
_photoUrl=value;
notifyListeners();
}
saveNotice(){
var newNotice=Notice(userName: userName,userPost: userPost,photoUrl: photoUrl,
date: date,noticeId: uuid.v4());
firestoreService.saveNotice(newNotice);
}
}
here is firestoreservice.dart
class FirestoreService {
Firestore _db=Firestore.instance;
Future<void> saveNotice(Notice notice){
return _db.collection('notices').document(notice.noticeId).setData(notice.toMap());
}
}
image is saved in firebase storage, but the URL can't get to the database. it always shows 'null'.
Upvotes: 0
Views: 157
Reputation: 2802
Try this one to upload image/File and get it's Download URL
final StorageReference storageReferencem = FirebaseStorage()
.ref()
.child("Users/${DateTime.now().millisecondsSinceEpoch}");
final StorageUploadTask uploadTaskm =
storageReferencem.putFile(croppedFile);
await uploadTaskm.onComplete;
await storageReferencem.getDownloadURL().then((url) {
URL=url;
});
Upvotes: 1