Reputation: 283
//I am trying to do this
String? userAvaterUrl = null;
//so I can do this:
FutureBuilder(
future: getPicture(),
builder: (context, snapshot) {
if (snapshot.hasData || snapshot.data != null) {
return CircleAvatar(
radius: width * 0.18,
backgroundImage:
NetworkImage(snapshot.data.toString()),
);
} else {
return CircleAvatar(
radius: width * 0.18,
backgroundImage: AssetImage(
"assets/images/icons2.png"));
}
},
),
Upvotes: 0
Views: 208
Reputation: 574
this line:
if(snapshot.hasData || snapshot.data != null) {
...
would check if the document has data. if you need to get the userAvaterUrl
from the doc, you will need to use:
backgroundImage: NetworkImage(snapshot.data['userAvaterUrl'].toString()),
but remember that even if snapshot.hasData || snapshot.data != null
, snapshot.data['userAvaterUrl']
can be null and you may want to make sure that your app can handle that. so, instead, you may want to do this:
if(snapshot.hasData &&
snapshot.data != null &&
snapshot.data['userAvaterUrl'] != null &&
snapshot.data['userAvaterUrl'] is String
) {
... // return avatar with the network image
} else {
... // return avatar with the asset image
}
by the way, if you are using flutter 2 with null-safety, this:
String? userAvaterUrl = null;
would just be
String? userAvaterUrl; // no need to set to null
Upvotes: 1