I am uploading an image, storing it in storage, and then the downloard url is then stored in Firebase Firestore. The update in Firestore takes place successfully, however, my UI is not being updated, while using a StreamBuilder. I don’t get any error, the only issue is that my UI is not getting updated as I intended to.
Code Snippet as follows:
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)),
),
))
],
),
),