I m tring to save course details from the filled form to firebase-database by calling the below function but I m getting some error. Here is a part of my code:
Future<void>saveCourseDataToDb(courseName, courseDescription,coursePrice,yearsOfExp, cities) async{
var timeStamp = Timestamp.now().microsecondsSinceEpoch;
FirebaseUser teacher= FirebaseAuth.instance.currentUser as FirebaseUser;
CollectionReference _courses = Firestore.instance.collection('courses');
try{
_courses.document(timeStamp.toString()).setData({
'tutor': {'TeacherName': this.TeacherName, 'Teacherid': teacher.uid },
'courseName': courseName,
'courseDescription': courseDescription,
'coursePrice': coursePrice,
'yearsOfExp': yearsOfExp,
'cities': cities,
'category' : {'categoryName': this.selectedCategory, 'categoryImage': this.categoryImage},
//'certificate': certificate,
'published': false,
'courseId': timeStamp.toString(),
'courseImage': this.courseURL,
'certificate' : this.certificateURL
});
ErrorAlertDialog(message: 'Course Details saved successfully',);
}catch(e){
ErrorAlertDialog(message: '${e.toString()}',);
}
return null;
}
And when I declare teacher with Future I get an error related to uid, here is a picture about the same.
Any help would be really appreciated!!
CodePudding user response:
FirebaseUser is a Future, which means you have to "wait" for it to arrive. Try like this
FirebaseUser teacher = await FirebaseAuth.instance.currentUser;
CodePudding user response:
instead of using
Future<FirebaseUser>
use
Future<User>
FirebaseUser has been changed to User long time ago & also use await before FirebaseAuth.instance.currentUser;
hope this may work