如何在 Flutter 中使用来自 Firebase 的数据预填充 TextField?

问题描述 投票:0回答:0

我有一个页面使用

Listview.Builder
从 firebase 返回数据列表,当我点击其中一个项目时,我会转到相应数据的详细信息页面。在此详细信息页面中,我有一个
FloatingActionButton
返回一个
AlertDialog
以更改现有数据详细信息。在这个对话框中,我有几个
TextField
小部件我想预填充。我尝试使用下面的代码,但它似乎不起作用。有人可以帮助我吗?

void populate(){
    FirebaseFirestore.instance
        .collection('OutletWillyJKT')
        .doc('Kode Outlet')
        .get()
        .then((DocumentSnapshot snapshot) {
      setState(() {
        namaOutletController.text = snapshot['Nama Outlet'];
        alamatOutletController.text = snapshot['Alamat Outlet'];
        noTlpOutletController.text = snapshot['Nomor Telepon Outlet'];
      });
    });
  }
  
  @override
  void initState() {
    populate();
    super.initState();
  }

这是

AlertDialog

的代码
Future<void> _showMyDialog(title) async {
    return showDialog<void>(
      context: context,
      barrierDismissible: true, // user must tap button!
      builder: (BuildContext context) {
        return AlertDialog(
          title: const Text('Change Outlet Information'),
          content: StatefulBuilder(
              builder: (BuildContext context, StateSetter setState) {
            return SingleChildScrollView(
              child: ListBody(
                children: <Widget>[
                  Column(
                    mainAxisAlignment: MainAxisAlignment.start,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: <Widget>[
                      TextField(
                        controller: namaOutletController,
                        decoration:
                            const InputDecoration(labelText: "Nama Outlet"),
                      ),
                      TextField(
                        controller: alamatOutletController,
                        decoration:
                            const InputDecoration(labelText: "Alamat Outlet"),
                      ),
                      TextField(
                        controller: noTlpOutletController,
                        decoration: const InputDecoration(
                            labelText: "Nomor Telepon Outlet"),
                        keyboardType: TextInputType.number,
                        inputFormatters: <TextInputFormatter>[
                          FilteringTextInputFormatter.digitsOnly
                        ], // O
                      ),
                      const SizedBox(
                        height: 10,
                      ),
                      ElevatedButton(
                        onPressed: () {
                          imageDialog();
                        },
                        child: const Text('Upload Photo'),
                      ),
                      const SizedBox(
                        height: 10,
                      ),
                      //if image not null show the image
                      //if image null show text
                      image != null
                          ? Padding(
                              padding:
                                  const EdgeInsets.symmetric(horizontal: 20),
                              child: ClipRRect(
                                borderRadius: BorderRadius.circular(8),
                                child: Image.file(
                                  //to show image, you type like this.
                                  File(image!.path),
                                  fit: BoxFit.cover,
                                  width: MediaQuery.of(context).size.width,
                                  height: 300,
                                ),
                              ),
                            )
                          : const Text(
                              "No Image",
                              style: TextStyle(fontSize: 20),
                            ),
                    ],
                  )
                ],
              ),
            );
          }),
          actions: <Widget>[
            TextButton(
              child: const Text('Change'),
              onPressed: () {
                changeData(title);
              },
            ),
          ],
        );
      },
    );
  }
flutter firebase google-cloud-firestore textfield
© www.soinside.com 2019 - 2024. All rights reserved.