I'm writing a code to save data in firestore, but it saves other data without image url.
Here is my code:
import 'package:flutter/material.dart';
import 'package:flutter_learning_app/Models/notice.dart';
import 'package:flutter_learning_app/Services/crud.dart';
import 'package:uuid/uuid.dart';
class NoticeProvider with ChangeNotifier {
final firestoreService=FirestoreService();
String _userName;
String _userPost;
String _date;
String _photoUrl;
var uuid=Uuid();
//setters
changeUserName(String value){
_userName=value;
notifyListeners();
}
changeUserPost(String value){
_userPost=value;
notifyListeners();
}
changeDate(String value){
_date=value;
notifyListeners();
}
changePhotoUrl(value){
_photoUrl=value;
print('$photoUrl');
notifyListeners();
}
//getters
String get userName => _userName;
String get userPost => _userPost;
String get date => _date;
String get photoUrl => _photoUrl;
saveNotice(){
print('$photoUrl');
print('$userName');
var newNotice=Notice(userName:userName,userPost:userPost,
date:date,photoUrl:photoUrl , noticeId: uuid.v4());
firestoreService.saveNotice(newNotice);
}
}
I think saveNotice()
function is executing before changePhotoUrl(value)
function, it print value to username
but it prints photourl
null, after that it prints url. I think it is the result of the print statement which is in changePhotoUrl(value)
.
This is the place where I get download photo url:
File noticepic;
Future getNotice()async{
var tempImage = await ImagePicker.pickImage(source: ImageSource.gallery);
setState(() {
noticepic=tempImage ;
});
}
uploadNotice()async{
String fileName=basename(noticepic.path);
final StorageReference firebaseStorageRef=FirebaseStorage().ref().child
('cisnotices/$fileName');
final StorageUploadTask task = firebaseStorageRef.putFile(noticepic);
await task.onComplete;
await firebaseStorageRef.getDownloadURL().then((url) {
noticeProvider.changePhotoUrl(url.toString());
});
}
NoticeProvider noticeProvider=NoticeProvider();
This is the place where I call getimage and upload image functions, then I called saveNotice()
function:
Padding( padding: const EdgeInsets.only(left: 280),
child: Container(
child: IconButton(
icon:Icon(
Icons.add_photo_alternate
),
color: Colors.grey[700],
iconSize: 40,
onPressed:(){
getNotice().then((f) async => await uploadNotice());
}
),
),
),
SizedBox(height:10.0),
Container(
height: 30.0,
width: 100.0,
child: RaisedButton(
onPressed: (){
noticeProvider.saveNotice();
},
child: Text('Upload'),
color: Colors.green[400],
),
)
This is the output:
I/flutter (19698): null
I/flutter (19698): aja yogan
I/System.out(19698): (HTTPLog)-Static: isSBSettingEnabled false
I/System.out(19698): (HTTPLog)-Static: isSBSettingEnabled false
D/NetworkManagementSocketTagger(19698): tagSocket(116) with statsTag=0xffffffff, statsUid=-1
I/System.out(19698): (HTTPLog)-Static: isSBSettingEnabled false
I/System.out(19698): (HTTPLog)-Static: isSBSettingEnabled false
I/flutter (19698): https://firebasestorage.googleapis.com/v0/b/learningapp-313e5.appspot.com/o/cisnotices%2Fimage_picker5085188822864985108.jpg?alt=media&token=8eccc3c7-14b5-4820-916e-5f1e4a67954e
User contributions licensed under CC BY-SA 3.0