Не удалось обновить данные в flutter firebase — 2021

# #firebase #flutter #google-cloud-firestore

Вопрос:

Я новичок на огневой базе. Я начал учиться на YouTube и документах. Поскольку обновления flutter изменились в этом году, на YouTube не так много видео или блогов, которые были бы мне полезны. Здесь я извлек данные из облака firebase и отобразил textFormField их примерно так. Но я не могу обновить данные. Может ли кто-нибудь помочь мне, в чем проблема и как я могу ее решить.

  child: StreamBuilder<QuerySnapshot>(
          stream: FirebaseFirestore.instance
              .collection("personalDetail")
              .snapshots(),
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              return ListView.builder(
                itemCount: 1,
                itemBuilder: (context, index) {
                  QueryDocumentSnapshot x = snapshot.data!.docs[index];
                  return Container(
                    child: Column(
                      children: [
                        Center(
                          child: Stack(
                            children: [
                              Container(
                                width: 130,
                                height: 130,
                                decoration: BoxDecoration(
                                  border: Border.all(
                                      width: 4, color: Colors.white),
                                  boxShadow: [
                                    BoxShadow(
                                      spreadRadius: 2,
                                      blurRadius: 10,
                                      color: Colors.black.withOpacity(0.1),
                                    )
                                  ],
                                  shape: BoxShape.circle,
                                  image: DecorationImage(
                                    fit: BoxFit.cover,
                                    image: AssetImage(
                                        'assets/images/Profile.jpg'),
                                  ),
                                ),
                              ),
                              Positioned(
                                bottom: 0,
                                right: 0,
                                child: Container(
                                  height: 40,
                                  width: 40,
                                  decoration: BoxDecoration(
                                      shape: BoxShape.circle,
                                      border: Border.all(
                                        width: 4,
                                        color: Colors.white,
                                      ),
                                      color: Colors.green.shade400),
                                  child: Icon(
                                    Icons.camera_alt,
                                    color: Colors.white,
                                  ),
                                ),
                              )
                            ],
                          ),
                        ),
                        SizedBox(
                          height: 30,
                        ),
                        TextFormField(
                          maxLength: 20,
                          style: TextStyle(
                            color: Colors.black,
                            fontSize: 15,
                          ),
                          controller:
                              TextEditingController(text: x['firstName']),
                          decoration: InputDecoration(
                            labelText: "First Name",
                            labelStyle: TextStyle(
                                fontWeight: FontWeight.w800,
                                color: Colors.black,
                                fontSize: 23),
                            floatingLabelBehavior:
                                FloatingLabelBehavior.always,
                          ),
                        ),
                        TextFormField(
                          maxLength: 20,
                          style: TextStyle(
                            color: Colors.black,
                            fontSize: 15,
                          ),
                          controller:
                          TextEditingController(text: x['lastName']),
                          decoration: InputDecoration(
                            labelText: "Last Name",
                            labelStyle: TextStyle(
                                fontWeight: FontWeight.w800,
                                color: Colors.black,
                                fontSize: 23),
                            floatingLabelBehavior:
                                FloatingLabelBehavior.always,
                          ),
                        ),
                        TextFormField(
                          maxLength: 15,
                          style:
                              TextStyle(color: Colors.black, fontSize: 15),
                          controller:
                              TextEditingController(text: x['middleName']),
                          decoration: InputDecoration(
                            labelText: "Middle Name",
                            labelStyle: TextStyle(
                                fontWeight: FontWeight.w800,
                                color: Colors.black,
                                fontSize: 23),
                            floatingLabelBehavior:
                                FloatingLabelBehavior.always,
                          ),
                        ),
                        TextFormField(
                          maxLength: 10,
                          keyboardType: TextInputType.phone,
                          inputFormatters: [
                            FilteringTextInputFormatter.digitsOnly
                          ],
                          style:
                              TextStyle(color: Colors.black, fontSize: 15),
                          controller:
                              TextEditingController(text: x['phone']),
                          decoration: InputDecoration(
                            labelText: "Phone Number",
                            labelStyle: TextStyle(
                                fontWeight: FontWeight.w800,
                                color: Colors.black,
                                fontSize: 23),
                            floatingLabelBehavior:
                                FloatingLabelBehavior.always,
                          ),
                        ),
                        TextFormField(
                          initialValue: x['email'],
                          enabled: false,
                          style: TextStyle(
                            color: Colors.black,
                            fontSize: 15,
                          ),
                          decoration: InputDecoration(
                            labelText: "Email",
                            labelStyle: TextStyle(
                                fontWeight: FontWeight.w800,
                                color: Colors.black,
                                fontSize: 23),
                            floatingLabelBehavior:
                                FloatingLabelBehavior.always,
                          ),
                        ),
                        SizedBox(
                          height: 30,
                        ),
                        Align(
                          child: ElevatedButton(
                            onPressed: () {
                              FirebaseFirestore.instance
                                  .collection('personalDetail')
                                  .doc('pxylzgi6iaIdV3uswzzK')
                                  .update({
                                "firstName": TextEditingController(text: x['firstName']),
                                "lastName": TextEditingController(text: x['lastName']),
                                "middleName":TextEditingController(text: x['middleName']),
                                "phone":TextEditingController(text: x['phone']),
                              });
                              Fluttertoast.showToast(
                                msg: "Profile updated Successfully",
                                toastLength: Toast.LENGTH_SHORT,
                                gravity: ToastGravity.BOTTOM,
                                textColor: Colors.black,
                                backgroundColor: Colors.green.shade400,
                              );
                              Navigator.pushNamed(
                                  context, "/profilePage");
                            },
                            child: Text(
                              "Save Details",
                              style: TextStyle(
                                  fontSize: 15,
                                  letterSpacing: 2,
                                  color: Colors.black),
                            ),
                            style: ElevatedButton.styleFrom(
                                fixedSize: Size(250, 40),
                                primary: Colors.green.shade500,
                                padding: EdgeInsets.symmetric(
                                  horizontal: 50,
                                ),
                                shape: RoundedRectangleBorder(
                                    borderRadius:
                                        BorderRadius.circular(10))),
                          ),
                        )
                      ],
                    ),
                  );
                },
              );
            } else {
              Center(
                child: CircularProgressIndicator(),
              );
            }
            throw "";
          },
        ),
 

Пожалуйста, помогите, я пробую разные видео на YouTube, но ни одно не помогает.

Ответ №1:

Вы проверили разрешение Firebase на чтение и запись? Обратитесь к этой документации для получения дополнительной помощи https://firebase.google.com/docs/database/web/read-and-write

Комментарии:

1. Можете ли вы немного объяснить, что не так в моем коде