Nabia Salman
Nabia Salman

Reputation: 632

Flutter Error: type '_Uri' is not a subtype of type 'String'

I am getting this error on saving retrieving URL of the picture from firebase real-time database

type '_Uri' is not a subtype of type 'String'

what I did was first I stored the image URL on firebase storage and then I saved the URL in my firebase real-time database under the current user information, but when I am accessing profile image URL from Db it gives me the error mentioned above.

My screen view looks like this, in which I want to show an image that the user selects as a current user profile image until the user changes the image.

enter image description here

My code for function is:

FirebaseAuth firebaseAuth = FirebaseAuth.instance;
  final ref = FirebaseDatabase.instance.reference();
  final fb = FirebaseDatabase.instance;

Future<Uri> getPicture() async {
    User cuser = await firebaseAuth.currentUser;
    return ref
        .child('User_data')
        .child(cuser.uid)
        .once()
        .then((DataSnapshot snap) {
      final String profileURL = snap.value['profile_photo'].toString();
      var myUri = Uri.parse(profileURL);
      print(myUri);
      return myUri;
    });
  }

Code where Image is shown and retrieved from DB:

Center(
              child: Stack(
                alignment: Alignment.center,
                children: <Widget>[
                  FutureBuilder(
                    future: getPicture(),
                      builder: (context, snapshot)
                      {
                      if (snapshot.hasData)
                        {
                          return CircleAvatar(
                            radius: 50,
                            backgroundImage: snapshot.data == null
                                ? AssetImage("assets/images/avatar.jpg")
                                : Image.network(snapshot.data),
                          );
                        }

                      }
                  ),
                  Positioned(
                    bottom: 1,
                    right: 1,
                    child: IconButton(
                      icon: Icon(
                        Icons.add_circle,
                        color: const Color(0xffd4d411),
                        size: 30,
                      ),
                      onPressed: () {
                        showModalBottomSheet(
                          context: context,
                          builder: ((builder) => bottomSheet(context)),
                        );
                      },
                      //color: const Color(0xffd4d411),
                    ),
                  )
                ],
              ),
            ),

Upvotes: 0

Views: 317

Answers (1)

Nisanth Reddy
Nisanth Reddy

Reputation: 6405

Image.network takes a String as it's positional paramter. But you are providing it a Uri object.

Change your getPicture to this,

Future<String> getPicture() async {
  User cuser = await firebaseAuth.currentUser;
  return ref
    .child('User_data')
    .child(cuser.uid)
    .once()
    .then((DataSnapshot snap) {
      return snap.value['profile_photo'].toString();
    });
}

Now, you will receive a String in snapshot.data.

Next, change your Image.Network into a NetworkImage like this,

backgroundImage: snapshot.data == null
  ? AssetImage("assets/images/avatar.jpg")
  : NetworkImage(snapshot.data),

Upvotes: 1

Related Questions