【问题标题】:How to fetch data from firestore and set it to Flutter TextFormField如何从 firestore 获取数据并将其设置为 Flutter TextFormField
【发布时间】:2021-03-29 17:43:20
【问题描述】:

我真的是 firebase 的新手,我正在尝试从云 firestore 中获取数据并将其设置为一个 textformfield,但我的努力并没有取得成效。我想从 firestore 数据库中获取批号并将其设置为我的应用程序中的 _batchTextEditingController。这是查看我的云 Firestore 数据库的链接 [Click here to view firestore database ] 下面是我的代码。

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),
          ],
        ),
      ),
    );
  }

【问题讨论】:

  • 我不知道您是如何与 Firebase 交互的。你能发布那部分代码吗?

标签: android flutter google-cloud-firestore


【解决方案1】:

使用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(),
    );
  },
);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-03
    • 1970-01-01
    • 2019-02-26
    • 2018-10-14
    相关资源
    最近更新 更多