Serhebunse
Serhebunse

Reputation: 43

Save the image even the application restart

I have a problem. Everytime when I search a image from my gallery for my profilepicture its work. even when I pressed the log out button and log in again there is still my profilpicture. But when I closed the app and I restart the app then the profilepicture is gone. Does anyone know how to save the URL correctly?

 Future uploadPic(BuildContext context) async {
    String fileName = basename(_image.path);
    StorageReference firebaseStorageRef = FirebaseStorage.instance.ref().child(
        fileName);
    StorageUploadTask uploadTask = firebaseStorageRef.putFile(_image);
    StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;
    var downUrl = await (await uploadTask.onComplete).ref.getDownloadURL();
    Constants.URL_Profil_Picture = downUrl.toString();


    setState(() {
      print('Profile Picture uploaded');
      Scaffold.of(context).showSnackBar(
          SnackBar(content: Text('Profile Picture Uploaded')));
    });
  }
  image: (_image != null) ? FileImage(
                              _image)
                              : NetworkImage(Constants.URL_Profil_Picture),

Upvotes: 1

Views: 48

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 600126

This seems the cause of the problem:

Constants.URL_Profil_Picture = downUrl.toString();

My guess it that when you restart the app, the Constants.URL_Profil_Picture is set to its hard-coded value again.

You'll need to store the URL somewhere that survives application restarts. The common approaches for that are:

  1. Store the URL in shared preferences, or some other form of local storage. This makes the URL available only on the device where it was uploaded from.

  2. Store the URL in a cloud database, such as Cloud Firestore or Realtime Database. This is the most common option, as it gives you the most flexibility.

  3. Given that you're talking about user profiles: if you're using Firebase Authentication to sign in the user, and this image is their profile picture, you can also store it in the user profile.

Upvotes: 1

Related Questions