Nabia Salman
Nabia Salman

Reputation: 632

Flutter Error: Invalid argument(s): No host specified in URI file:///null

I want to access my image from Firebase real-time database. But I haven't saved the image on Db yet, so it should show the default image but instead, I am getting this blue image that is being shown with Welcome Back Hania. I am getting this image:

enter image description here

and my Db has no image URL yet:

enter image description here

So incase no URL is saved inside DB, it should show my default image like this:

enter image description here

My code for getting image in Future function is:

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();
    });
  }

Code where Future Builder is used for getting Image from DB:

 Container(
                alignment: Alignment.centerRight,
                width: size.width,
                padding: EdgeInsets.symmetric(horizontal: 33),
                child: CircleAvatar(
                  radius: 46,
                  backgroundColor: Colors.white,
                  child: FutureBuilder(
                    future: getPicture(),
                    builder: (context, snapshot) {
                      if (snapshot.hasData) {
                        return CircleAvatar(
                          radius: 40,
                          backgroundImage: NetworkImage(snapshot.data),
                        );
                      } else {
                        return CircleAvatar(
                            radius: 40,
                            backgroundImage:
                                AssetImage("assets/images/avatar.jpg"));
                      }
                    },
                  ),
                ),
              ),

I will store the image later when Editing Profile Info of USer, for now, I am not adding it. So what will the solution to my error. Please help me out as I am new to Flutter

Error:

════════ Exception caught by image resource service ════════════════════════════════════════════════
The following ArgumentError was thrown resolving an image codec:
Invalid argument(s): No host specified in URI file:///null

When the exception was thrown, this was the stack: 
#0      _HttpClient._openUrl (dart:_http/http_impl.dart:2425:9)
#1      _HttpClient.getUrl (dart:_http/http_impl.dart:2346:48)
#2      NetworkImage._loadAsync (package:flutter/src/painting/_network_image_io.dart:89:59)
#3      NetworkImage.load (package:flutter/src/painting/_network_image_io.dart:50:14)
#4      ImageProvider.resolveStreamForKey.<anonymous closure> (package:flutter/src/painting/image_provider.dart:503:13)
...
Image provider: NetworkImage("null", scale: 1.0)
Image key: NetworkImage("null", scale: 1.0)
════════════════════════════════════════════════════════════════════════════════════════════════════

Upvotes: 1

Views: 7432

Answers (1)

mkobuolys
mkobuolys

Reputation: 5343

The problem is this line:

return snap.value['profile_photo'].toString();

Since snap.value['profile_photo'] is null, then null.toString() results in "null" - a String value of null.

What you need to do is to adjust this statement a little bit:

return snap.value['profile_photo']?.toString();

Now, if snap.value['profile_photo'] is null, then the actual null value would be returned from this future and not a String representation of it.

Upvotes: 2

Related Questions