简体   繁体   English

如何从 firestore 获取数据并将其设置为 Flutter TextFormField

[英]How to fetch data from firestore and set it to Flutter TextFormField

I'm really new to firebase, i'm trying to fetch data from the cloud firestore and set it to a textformfield in flutter but my efforts have not been productive.我对 firebase 真的很陌生,我正在尝试从云 Firestore 中获取数据并将其设置为 flutter 中的 textformfield,但我的努力没有成效。 I want to get a batch number from firestore database and set it to the _batchTextEditingController in my application.我想从 firestore 数据库中获取批号并将其设置为我的应用程序中的 _batchTextEditingController。 Here is the link to view my cloud firestore database [这是查看我的云 Firestore 数据库的链接 [ Click here to view firestore database ] and below is my code.单击此处查看 Firestore 数据库],以下是我的代码。

 class _RegisterState extends State<Register> { final TextEditingController _nameTextEditingController = TextEditingController(); final TextEditingController _emailTextEditingController = TextEditingController(); final TextEditingController _passwordTextEditingController = TextEditingController(); final TextEditingController _cPasswordTextEditingController = TextEditingController(); //This is the textfield i want to set... please help TextEditingController _batchTextEditingController = TextEditingController(); final GlobalKey<FormState> _formKey = GlobalKey<FormState>(); String userImageUrl = ""; File _imageFile; @override Widget build(BuildContext context) { double _screenWidth = MediaQuery.of(context).size.width; double _screenheight = MediaQuery.of(context).size.height; return SingleChildScrollView( child: Container( child: Column( mainAxisSize: MainAxisSize.max, children: [ SizedBox(height: 10.0), InkWell( onTap: () => _selectAndPickImage(), child: CircleAvatar( radius: _screenWidth * 0.15, backgroundColor: Colors.white, backgroundImage: _imageFile == null? null: FileImage(_imageFile), child: _imageFile == null? Icon( Icons.add_a_photo_sharp, size: _screenWidth * 0.15, color: Colors.grey, ): null, ), ), SizedBox(height: 8.0), Form( key: _formKey, child: Column( children: [ CustomTextField( controller: _nameTextEditingController, data: Icons.person, hintText: "Name", isObsecure: false, ), CustomTextField( controller: _emailTextEditingController, data: Icons.email, hintText: "Email", isObsecure: false, ), CustomTextField( controller: _passwordTextEditingController, data: Icons.person, hintText: "Password", isObsecure: true, ), CustomTextField( controller: _cPasswordTextEditingController, data: Icons.person, hintText: "Confirm password", isObsecure: true, ), CustomTextField( controller: _batchTextEditingController, data: Icons.book, hintText: "Batch", isObsecure: false, ), ], ), ), RaisedButton( onPressed: () { uploadAndsaveImage(); }, color: Colors.pink, child: Text( "Signup", style: TextStyle(color: Colors.white), ), ), SizedBox(height: 30), Container( height: 4.0, width: _screenWidth * 0.8, color: Colors.pink, ), SizedBox(height: 15), ], ), ), ); }

Use a FutureBuilder !使用FutureBuilder

 FutureBuilder<DocumentSnapshot>(
  future: FirebaseFirestore.instance.collection('data').doc('docID').get(),//Get the data from cloud firestore
  builder: (BuildContext context, AsyncSnapshot snapshot) {
    if (snapshot.hasData) {
      return TextFormField(
        initialValue: snapshot.data,//Inserts into the form as initial value
      );
    }
    return Center(
      child: CircularProgressIndicator(),
    );
  },
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM