'flutter firestore "in the user profile screen some data doesn't appear when fetching user data"
I was using Firestore database for the flutter app, so on the user profile screen, some data"mobile&email" doesn't appear when fetching user data.
I don't understand why it does not appear cuz I used the same code with driver user and student and it's worked.

profile screen, the third field for mobile number and the fourth for email
this's Profile screen code:
class SupervisorProfileScreen extends StatefulWidget {
const SupervisorProfileScreen({Key? key}) : super(key: key);
@override
State<SupervisorProfileScreen> createState() => _SupervisorProfileScreenState();
}
class _SupervisorProfileScreenState extends State<SupervisorProfileScreen> {
TextEditingController usernameController = TextEditingController();
TextEditingController nameController = TextEditingController();
TextEditingController mobileController = TextEditingController();
TextEditingController emailController = TextEditingController();
TextEditingController passwordController = TextEditingController();
SupervisorModel? supervisorModel;
@override
void initState() {
// TODO: implement initState
super.initState();
fetchLocalSupervisor();
}
void fetchLocalSupervisor() async {
SupervisorModel? user = await getLocalSuperVisor();
if (user != null) {
setState(() {
supervisorModel = user;
usernameController.text = user.userName ?? "";
nameController.text = user.name ?? "";
mobileController.text = user.mobile ?? "";
emailController.text = user.email ?? "";
passwordController.text = user.password ?? "";
});
}
}
/*
*/
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: SafeArea(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
SizedBox(
height: 10.h,
),
Row(
children: [
InkWell(
onTap: () {
Get.back();
},
child: const Icon(
Icons.arrow_back_ios,
color: Colors.black,
),
),
const Expanded(
child: SizedBox(),
),
const Text(
"الملف الشخصي",
style: TextStyle(color: Color(0xff263238), fontSize: 20),
),
],
),
SizedBox(
height: 8.h,
),
Expanded(
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
SizedBox(
width: 150.w,
height: 150.w,
child: Image.asset("assets/images/img_user2.png"),
),
SizedBox(
height: 10.h,
),
Padding(
padding: const EdgeInsets.only(bottom: 10),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
SizedBox(
height: 33,
width: 235.w,
child: TextField(
controller: usernameController,
textAlign: TextAlign.right,
readOnly: true,
decoration: InputDecoration(
contentPadding: const EdgeInsets.symmetric(
vertical: 0.0, horizontal: 16),
enabledBorder: const OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(25)),
borderSide: BorderSide(
color: Colors.transparent, width: 0.0),
),
focusedBorder: const OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(25)),
borderSide: BorderSide(
color: Colors.transparent, width: 0.0),
),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(25.0),
borderSide: const BorderSide(
color: Colors.transparent, width: 0.0),
),
filled: true,
hintText: "",
hintStyle: TextStyle(
fontSize: 14.sp,
color: const Color(0xffB6B7B7)),
fillColor: const Color(0xffF2F2F2)),
),
),
const Text(
"اسم المسخدم",
style: TextStyle(
color: Color(0xff263238), fontSize: 14),
),
],
),
),
Padding(
padding: const EdgeInsets.only(bottom: 10),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
SizedBox(
height: 33,
width: 235.w,
child: TextField(
controller: nameController,
readOnly: true,
textAlign: TextAlign.right,
keyboardType: TextInputType.number,
decoration: InputDecoration(
contentPadding: const EdgeInsets.symmetric(
vertical: 0.0, horizontal: 16),
enabledBorder: const OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(25)),
borderSide: BorderSide(
color: Colors.transparent, width: 0.0),
),
focusedBorder: const OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(25)),
borderSide: BorderSide(
color: Colors.transparent, width: 0.0),
),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(25.0),
borderSide: const BorderSide(
color: Colors.transparent, width: 0.0),
),
filled: true,
hintText: "",
hintStyle: TextStyle(
fontSize: 14.sp,
color: const Color(0xffB6B7B7)),
fillColor: const Color(0xffF2F2F2)),
),
),
const Text(
"الاسم",
style: TextStyle(
color: Color(0xff263238), fontSize: 14),
),
],
),
),
Padding(
padding: const EdgeInsets.only(bottom: 10),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
SizedBox(
height: 33,
width: 235.w,
child: TextField(
controller: mobileController,
inputFormatters: [
LengthLimitingTextInputFormatter(10),
],
keyboardType: TextInputType.number,
style: const TextStyle(fontFamily: "Roboto"),
textAlign: TextAlign.right,
decoration: InputDecoration(
prefixIcon: const Icon(Icons.edit, color: AppConstants.lightGreen,),
contentPadding: const EdgeInsets.symmetric(
vertical: 0.0, horizontal: 16),
enabledBorder: const OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(25)),
borderSide: BorderSide(
color: Colors.transparent, width: 0.0),
),
focusedBorder: const OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(25)),
borderSide: BorderSide(
color: Colors.transparent, width: 0.0),
),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(25.0),
borderSide: const BorderSide(
color: Colors.transparent, width: 0.0),
),
filled: true,
hintText: "",
hintStyle: TextStyle(
fontSize: 14.sp,
color: const Color(0xffB6B7B7)),
fillColor: const Color(0xffF2F2F2)),
),
),
const Text(
"رقم الجوال",
style: TextStyle(
color: Color(0xff263238), fontSize: 14),
),
],
),
),
Padding(
padding: const EdgeInsets.only(bottom: 10),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
SizedBox(
height: 33,
width: 235.w,
child: TextField(
controller: emailController,
keyboardType: TextInputType.emailAddress,
style: const TextStyle(fontFamily: "Roboto"),
textAlign: TextAlign.right,
decoration: InputDecoration(
prefixIcon: const Icon(Icons.edit, color: AppConstants.lightGreen,),
contentPadding: const EdgeInsets.symmetric(
vertical: 0.0, horizontal: 16),
enabledBorder: const OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(25)),
borderSide: BorderSide(
color: Colors.transparent, width: 0.0),
),
focusedBorder: const OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(25)),
borderSide: BorderSide(
color: Colors.transparent, width: 0.0),
),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(25.0),
borderSide: const BorderSide(
color: Colors.transparent, width: 0.0),
),
filled: true,
hintText: "",
hintStyle: TextStyle(
fontSize: 14.sp,
color: const Color(0xffB6B7B7)),
fillColor: const Color(0xffF2F2F2)),
),
),
const Text(
"البريد الالكتروني",
style: TextStyle(
color: Color(0xff263238), fontSize: 14),
),
],
),
),
Padding(
padding: const EdgeInsets.only(bottom: 10),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
SizedBox(
height: 33,
width: 235.w,
child: TextField(
controller: passwordController,
style: const TextStyle(fontFamily: "Roboto"),
textAlign: TextAlign.right,
obscureText: true,
decoration: InputDecoration(
prefixIcon:
const Icon(Icons.edit, color: AppConstants.lightGreen,),
contentPadding: const EdgeInsets.symmetric(
vertical: 0.0, horizontal: 16),
enabledBorder: const OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(25)),
borderSide: BorderSide(
color: Colors.transparent, width: 0.0),
),
focusedBorder: const OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(25)),
borderSide: BorderSide(
color: Colors.transparent, width: 0.0),
),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(25.0),
borderSide: const BorderSide(
color: Colors.transparent, width: 0.0),
),
filled: true,
hintText: "",
hintStyle: TextStyle(
fontSize: 14.sp,
color: const Color(0xffB6B7B7)),
fillColor: const Color(0xffF2F2F2)),
),
),
const Text(
"كلمة المرور",
style: TextStyle(
color: Color(0xff263238), fontSize: 14),
),
],
),
),
SizedBox(
height: 10.h,
),
Row(
children: [
InkWell(
onTap: () {
if (validateEmail(emailController.text)==true) {
Alert(
context: context,
type: AlertType.warning,
desc: "هل أنت متأكد من حفظ البيانات ؟",
buttons: [
DialogButton(
radius: BorderRadius.circular(20),
width: 90,
height: 34,
child: const Text(
"نعم",
style: TextStyle(
color: Colors.black,
fontSize: 20),
),
onPressed: () async {
Navigator.pop(context);
bool sts = await updateSupervisorData(
supervisorModel?.uid ?? "",
nameController.text,
mobileController.text,
emailController.text,
passwordController.text,);
if (sts) {
Fluttertoast.showToast(
msg: "تم الحفظ بنجاح");
} else {
Fluttertoast.showToast(
msg:
"عذراً حصل خطأ، يرجى المحاولة لاحقاً");
}
},
color: AppConstants.lightGreen,
),
],
).show();
};
},
child: Container(
width: 90,
height: 34,
decoration: BoxDecoration(
color: const Color(0xff9BF1AF),
borderRadius: BorderRadius.circular(28),
),
child: const Center(
child: Text(
"حفظ", //save
style: TextStyle(
color: Color(0xff263238), fontSize: 16),
),
),
),
),
const Expanded(
child: SizedBox(),
),
],
),
],
),
))
],
),
),
),
);
}
}
and this's the body of function getLocalSuperVisor
Future<SupervisorModel?> getLocalSuperVisor() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String? us = prefs.getString('supervisorObject');
if (us != null) {
SupervisorModel userObj = SupervisorModel.fromJson(json.decode(us));
return userObj;
} else {
return null;
}
}
supervisorModel:
class SupervisorModel {
String? uid;
String? mobile;
String? email;
String? userName;
String? password;
String? name;
SupervisorModel({
this.uid,
this.mobile,
this.email,
this.userName,
this.password,
this.name,
});
Map<String, dynamic> toJson() {
return {
FirebaseConstants.pathUid: uid,
FirebaseConstants.pathUserMobile: mobile,
FirebaseConstants.pathEmail: email,
FirebaseConstants.pathUserName: userName,
FirebaseConstants.pathUserPassword: password,
FirebaseConstants.pathName: name,
};
}
factory SupervisorModel.fromJson(Map<String, dynamic> json) =>
SupervisorModel(
uid: json[FirebaseConstants.pathUid],
mobile: json[FirebaseConstants.pathUserMobile],
email: json[FirebaseConstants.pathEmail],
userName: json[FirebaseConstants.pathUserName],
password: json[FirebaseConstants.pathUserPassword],
name: json[FirebaseConstants.pathName],
);
factory SupervisorModel.fromDocument(DocumentSnapshot doc) {
String uid = "";
String mobile = "";
String email = "";
String userName = "";
String password = "";
String name = "";
try {
uid = doc.id;
} catch (e) {}
try {
name = doc.get(FirebaseConstants.pathUserMobile);
} catch (e) {
}
try {
name = doc.get(FirebaseConstants.pathEmail);
} catch (e) {
// print(e.toString());
}
try {
userName = doc.get(FirebaseConstants.pathUserName);
} catch (e) {}
try {
password = doc.get(FirebaseConstants.pathUserPassword);
} catch (e) {}
try {
name = doc.get(FirebaseConstants.pathName);
} catch (e) {
// print(e.toString());
}
return SupervisorModel(
uid: uid,
mobile: mobile,
email: email,
userName: userName,
password: password,
name: name,
);
}
}
firebase Constants :
class FirebaseConstants {
// User Constants
static const pathStudentUserCollection = "students";
static const pathName = "name";
static const pathUserId = "id";
static const pathUserMobile = "mobile";
static const pathUserPassword = "password";
static const pathEmail = "email";
static const pathFatherName = "fatherName";
static const pathFatherMobile = "fatherMobile";
static const pathDirection = "direction";
static const pathAddress = "address";
static const pathDays = "days";
static const pathUid = "uid";
static const pathOnBus = "on_bus";
static const pathIsAttending = "is_attending";
// Supervisor
static const pathUserName = "user_name";
static const pathSupervisorsCollection = "supervisors";
// Driver
static const pathDriversCollection = "drivers";
}
the data is saved in the database correctly but when getting data it does not appear
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
