I am trying to retrieve an string url from FirebaseFirestore.
My code for UI is as follows
Container(
height: 65,
width: 65,
decoration: BoxDecoration(
border: Border.all(
width: 8,
color: const Color
.fromARGB(
255, 3, 54, 95)),
shape: BoxShape.circle,
color: Colors.black38),
child: ClipRRect(
borderRadius:
BorderRadius.circular(
22),
child: CachedNetworkImage(
imageUrl:
model.photoUrl,
fit: BoxFit.cover,
The code is trying to access a variable from the provider file, which is as follows:
photoUrl = AuthMethods().getPhotoUrl().toString();
//Authomethods file
Future<String> getPhotoUrl() async {
DocumentSnapshot snap = await FirebaseFirestore.instance
.collection('users')
.doc(FirebaseAuth.instance.currentUser!.uid)
.get();
final String photoUrl = (snap.data() as Map<String, dynamic>)['photoUrl'];
return photoUrl;
// return photoUrl;
}
3
Answers
getPhotoUrl()
is an async function and needs to be awaited to. It could only work if instead ofyou instead write
Although this can only be called like that in another async which you might not have
Async Data Retrieval:
getPhotoUrl
is an asynchronous function, meaning it returns aFuture
. The line where you’re assigningphotoUrl
isn’t awaiting that future, hence the problem.Provider/Model Updates: If you’re using a provider or any state management solution, ensure that you notify listeners when updating
photoUrl
so that the UI rebuilds with the new data.Using FutureBuilder: Since you’re dealing with asynchronous data retrieval, consider using
FutureBuilder
for better handling of the future state.Here’s how you can do it:
In your UI (where you want to display the image):
In your AuthMethods:
Ensure error handling if the
photoUrl
doesn’t exist in the document or if there are any other issues during the fetch.With this setup, the
FutureBuilder
will handle the lifecycle of your asynchronous function. It will show a loading indicator while fetching the data, display the image once the data is fetched, or handle errors if they occur.Before using your Url, you can call your Container by checking it to avoid asynchronous data pull problems. For example:
Alternative:
You can also review this answers: Invalid argument(s): No host specified in URI – Image.network
I hope I have helped. Enjoy your work.