本文介绍了如何从cloud_firestore获取数据并在flutter中将其显示在TextField中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Padding(
padding: const EdgeInsets.all(14.0),
child: Container(
child: TextFormField(
initialValue: fullName,
cursorColor: Colors.deepPurpleAccent,
decoration: InputDecoration(
hintText: 'Full Name',
focusedBorder: InputBorder.none,
focusColor: Colors.deepPurpleAccent,
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.deepPurpleAccent,
),
),
),
),
),
),
String fullName;
void _fetchUserData() async {
try {
FirebaseUser _currentUser = await FirebaseAuth.instance.currentUser();
String authid =_currentUser.uid;
Firestore.instance.collection('UserData').document('$authid').get().then((ds) {
if(ds.exists)
{
setState(() {
fullName = ds.data['FullName'];
});
}
});
} catch (e) {
print(e);
}
}
@override
void initState() {
super.initState();
_fetchUserData();
}
在此TextFormField中,我正在显示该值,但它显示为空
" In this TextFormField I am displaying that value but it is displaying null
我正在尝试以这种方式执行此操作,但是它在Android Emaluater TextFormField中显示了空值
" I am trying to do this in this manner but it is showing the null value in the Android Emaluater TextFormField
推荐答案
TextFormField的初始值仅用于初始化其TextController.TextFormField的值由TextEditingController维护.
TextFormField's initial value is only used to initialize its TextController.The TextFormField's value is maintained by the TextEditingController.
final controller = TextEditingController();
void _fetchUserData() async {
// do something
setState(() {
controller.text = "your text";
});
}
Widget build(BuildContext context) {
return TextFormField(
controller: controller,
);
}
void dispose() {
myController.dispose();
super.dispose();
}
这篇关于如何从cloud_firestore获取数据并在flutter中将其显示在TextField中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!