'How to make pickedImage from gallery or camera to effect immediately to all the pages in flutter?

I picked the image from camera and gallery in the edit profile page and I need to make it effective to the dashboard and drawer of the app immediately and also when user closes his app and comes back the photo must be there in the app. How can I achieve this.

Now I will store the image in a variable of type File, but it doesn't effect immediately and also it collpases when app is closed.



Solution 1:[1]

The Image you are picking, store the image in a variable of type File in setState({}).

Solution 2:[2]

First, your problem is divided into two parts:

  1. Once the image is picked, it should appear in the different places in your app.

One way to do that using the provider package as state management. Then create a class that extends change notifier. after that define a function that updates the image and notifies listeners. Something like this

class YourClass extends ChangeNotifier {
  String? _image;
    
  String? get image => _image;
    
  void updateImageAndSave(String providedImage) {
    _image = providedImage;
    saveTolocaleStorage(providedImage); //To save your image to any locale storage of your prefrence.
    notifyListeners();
  }
  void updateImage(String providedImage) {
    _image = providedImage;
    notifyListeners();
  } 
    
}

Note I use string because I actually provide the path, not the image itself. but you can change that.

After that, you use the consumer widget right above anywhere you want to be updated once the updateImage() function is called.

Note the image is nullable so you need to check if the image is null to show your temporary widget(your placeholder).

  1. Read the image from your local storage.

Once you have done the above part, read the image from your locale storage on the app startup. Then, update the image in your change notifier. This will display the image at all the different places you specified in your app

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Usama Khalid
Solution 2 magdy