-1

I want to retrieve the profile image of the user(URL). The user could have set image or not. So if the image field is empty, I get an exception. How could I check beforehand that image field is empty and I avoid crashing the app

test

firebaseFirestore.document("users/profiles/data/"+ad_poster_id).get().addOnCompleteListener(new OnCompleteListener() {

    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) { if(task.getResult().exists()){

            String profile_image = task.getResult().getString("image");
            if(profile_image!=null)
            Picasso.get().load(profile_image).into(circleImageView);
        }

    }
});
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • It's generally not a good idea to store an empty string when you're actually expecting a URL. If there is no data, consider simply not storing the field at all, or store null for the value in that field to indicate that no URL is available. – Doug Stevenson Apr 09 '19 at 05:56
  • Thanks a lot for your valuable feedback Respected Stevenson – Hamza Iqbal Apr 12 '19 at 16:33

1 Answers1

0

Please use this:

String profile_image = task.getResult().optString("image");

if(!profile_image.equals(""))
    Picasso.get().load(profile_image).into(circleImageView);
else
    Set Default Image