0

//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"));
                            }
                          },
                        ),
Ugocode
  • 235
  • 1
  • 3
  • 10

1 Answers1

1

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
brightknight08
  • 546
  • 5
  • 14
  • I want to do the: String? userAvaterUrl; so I can send a null value to firestore but its rejecting it. and also the solution above didn't work – Ugocode Oct 28 '21 at 20:42