About
Fetching the current user data from firebase.
Approach 1
var currentUser = FirebaseAuth.instance.currentUser;
if (currentUser != null) {
var user = FirebaseFirestore.instance
.collection("tblusers")
.doc(currentUser.uid)
.get()
.then((DocumentSnapshot documentSnapshot) {
if (documentSnapshot.exists) {
_controllerFirstName.text = documentSnapshot.data()["first_name"];
}
});
}
I have above lines of code that works fine. Below is the full code.
class AccountForm extends StatelessWidget {
var firstName = "";
TextEditingController _controllerFirstName = new TextEditingController();
var _accountFormKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
var currentUser = FirebaseAuth.instance.currentUser;
if (currentUser != null) {
var user = FirebaseFirestore.instance
.collection("tblusers")
.doc(currentUser.uid)
.get()
.then((DocumentSnapshot documentSnapshot) {
if (documentSnapshot.exists) {
_controllerFirstName.text = documentSnapshot.data()["first_name"];
}
});
}
return Form(
key: _accountFormKey,
autovalidateMode: AutovalidateMode.onUserInteraction,
child: Column(children: <Widget>[
Container(
margin: EdgeInsets.symmetric(horizontal: 10),
child: Column(children: [
TextFormField(
controller: _controllerFirstName,
validator: (value) {
if (value.isEmpty) {
return "Error";
}
},
),
ElevatedButton(
onPressed: () {
if (_accountFormKey.currentState.validate()) {}
},
child: Text("Submit"))
]))
]));
}
}
Then I tried to improve the code by adding the classes as below. New code looks like below. but, this does not populate first name
Approach 2
class UserModel {
var first_name;
UserModel(
{this.first_name});
}
class UserProvider {
UserModel userModel;
UserModel getUserData() {
var currentUser = FirebaseAuth.instance.currentUser;
if (currentUser != null) {
var user = FirebaseFirestore.instance
.collection("tblusers")
.doc(currentUser.uid)
.get()
.then((DocumentSnapshot documentSnapshot) {
if (documentSnapshot.exists) {
userModel = UserModel(
first_name: documentSnapshot.data()["first_name"]
}
});
}
}
}
Widget class
class AccountForm extends StatelessWidget {
var firstName = "";
TextEditingController _controllerFirstName = new TextEditingController();
var _accountFormKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
var provider = new UserProvider();
var result = provider.getUserData();
_controllerFirstName.text = result.first_name;
return Form(
key: _accountFormKey,
autovalidateMode: AutovalidateMode.onUserInteraction,
child: Column(children: <Widget>[
Container(
margin: EdgeInsets.symmetric(horizontal: 10),
child: Column(children: [
TextFormField(
controller: _controllerFirstName,
validator: (value) {
if (value.isEmpty) {
return MinMaxMessages.RequiredFirstName;
}
}
),
ElevatedButton(
onPressed: () {
if (_accountFormKey.currentState.validate()) {}
},
child: Text("Submit"))
]))
]));
}
}
question from:
https://stackoverflow.com/questions/65832175/approach-2-not-populating-firstname 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…