Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is Circle Avatar initially showing blue bg instead of default picture set, flutter?

I have side menu in an app in flutter. The circle avatar is supposed to show a default profile picture since user has not set one. When I first install it, it should look like this: enter image description here

But this is how it looks like when I first install it….enter image description here

I want it to show default profile photo instead of blue container.
This is the code :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

GestureDetector(
                              onTap: () async {
                                newphoto = await Navigator.push(
                                    context,
                                    MaterialPageRoute(
                                        builder: ((context) => ProfilePage(
                                            uname: context
                                                .read<ProfileDetails>()
                                                .name //username,
                                            ))));

                                saveimageside(newphoto);
                              },
                              child: profileimage != ''
                                  ? CircleAvatar(
                                      backgroundImage:
                                          FileImage(File(profileimage)),
                                      radius:
                                          MediaQuery.of(context).size.height *
                                              0.05,
                                    )
                                  : CircleAvatar(
                                      backgroundImage: const AssetImage(
                                          'assets/profile1.png'),
                                      radius:
                                          MediaQuery.of(context).size.height *
                                              0.05,
                                    )),

Outside the build I have :

String profileimage = ''; 

 saveimageside(path) async {
    SharedPreferences saveimageside = await SharedPreferences.getInstance();
    setState(() {
      saveimageside.setString("imagePathside", path);
    });
  }

  loadimageside() async {
    SharedPreferences saveimageside = await SharedPreferences.getInstance();
    setState(() {
      profileimage = saveimageside.getString("imagePathside").toString();
    });
  }

called loadimagesside in initState
This code is not fully written by me but someone before me.

The blue container is replaced by default image only when I use remove profile picture feature which sets the path to ” Thats okay but I want the app to show default when user first installs it also

>Solution :

You have checked if its empty but for example when you first opened the app without setting the path then loadimageSide() method is called which will get a null as a result and its saved in the variable profileImage. So now the value of profileImage is "null";

You can manage it when you get the image

loadimageside() async {
    SharedPreferences saveimageside = await SharedPreferences.getInstance();
    setState(() {
      profileimage = saveimageside.getString("imagePathside") ?? "";
    });
  }

Now if the image is null then it will assign an empty string.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading