为什么我的 UI 没有使用 StreamBuilder 进行更新?

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

我正在上传图像,将其存储在存储中,然后下载网址存储在 Firebase Firestore 中。 Firestore 中的更新成功进行,但是,在使用 StreamBuilder 时,我的 UI 没有更新。我没有收到任何错误,唯一的问题是我的 UI 没有按照我的预期进行更新。

代码片段如下:

 body: Padding(
          padding: const EdgeInsets.all(20.0),
          child: GestureDetector(
            onTap: () {
              FocusScope.of(context).unfocus();
            },
            child: ListView(
              children: [
                Center(
                  child: Stack(
                    children: [
                      StreamBuilder<QuerySnapshot<Map<String, dynamic>>>(
                        stream: FirebaseFirestore.instance
                            .collection('users')
                            .snapshots(),
                        builder: (context, snapshot) {
                          if (snapshot.connectionState ==
                              ConnectionState.waiting) {
                            return const CircularProgressIndicator();
                          } else if (snapshot.hasError) {
                            print(snapshot.error);
                            return const Text('Something went wrong');
                          } else if (!snapshot.hasData ||
                              snapshot.data!.docs.isEmpty) {
                            return const Text('No data available');
                          } else {
                            return CircleAvatar(
                              radius: 75,
                              backgroundImage: NetworkImage(
                                  widget.userData.profileImage.toString()),
                            );
                          }
                        },
                      ),
                      Positioned(
                          left: 107,
                          top: 107,
                          child: Container(
                            height: 40,
                            width: 40,
                            decoration: BoxDecoration(
                                border:
                                    Border.all(width: .7, color: Colors.white),
                                shape: BoxShape.circle,
                                color: const Color.fromARGB(255, 2, 143, 7)),
                            child: Center(
                              child: IconButton(
                                  color:
                                      const Color.fromARGB(255, 250, 250, 250),
                                  onPressed: () async {
                                    ImagePicker imagePicker = ImagePicker();

                                    String uniqueFileName = DateTime.now()
                                        .millisecondsSinceEpoch
                                        .toString();

                                    XFile? file = await imagePicker.pickImage(
                                        source: ImageSource.gallery);

                                    if (file == null) return;

                                    final image = await file.readAsBytes();

                                    Reference referenceRoot =
                                        FirebaseStorage.instance.ref();
                                    Reference referenceDirectory =
                                        referenceRoot.child('UserImages');
                                    Reference referenceImageToUpload =
                                        referenceDirectory
                                            .child(uniqueFileName);

                                    try {
                                      await referenceImageToUpload
                                          .putData(image);
                                      imageUrl = await referenceImageToUpload
                                          .getDownloadURL();
                                      debugPrint('ImageUrlReceived');

                                      UserModel newUserData = widget.userData
                                          .copyWith(profileImage: imageUrl);

                                      await newUserData.updateFirestore();
                                    } catch (error) {
                                      debugPrint('Some error occured');
                                      debugPrint(error.toString());
                                    }
                                  },
                                  icon: const Icon(Icons.edit)),
                            ),
                          ))
                    ],
                  ),
                ),

Firestore screenshot

flutter firebase google-cloud-firestore firebase-storage
1个回答
0
投票

我认为您在

NetworkImage
中使用了错误的链接。

                         return CircleAvatar(
                              radius: 75,
                              backgroundImage: NetworkImage(
                                  snapshot.data!.docs.first['profilImage']),  <------  here made changes.
                            );
© www.soinside.com 2019 - 2024. All rights reserved.