How to save an image permanently on Flutter after picking from Gallery or Camera? Am getting this error when am setting the picked image

Viewed 65

Am getting this error when am setting the picked image, future dynamic is not a subtype of string.

This is how am setting the image

enter image description here

This is how am saving the image path using shared preferences enter image description here

Where could I have gone wrong?

1 Answers

that's because getImagePathFromSharedPreferences doesn't return String?, it returns a Future

you either handle the async task in the initState or use a FutureBuilder

make sure to set the return type of getImagePathFromSharedPreferences

Future<String?> getImagePathFromSharedPreferences() async {
   // ...
}

I'm gonna use FutureBuilder:

FutureBuilder<String?>(
  future: getImagePathFromSharedPreferences(),
  initialData: null,
  builder: (
      BuildContext context,
      AsyncSnapshot<String?> snapshot,
      ) {
    if (snapshot.connectionState != ConnectionState.done) {
      return const CircularProgressIndicator();
    } else if (!snapshot.hasError) {
      return CircleAvatar(
        radius: 45.0,
        backgroundImage: _imageFile != null
          ? Image.file(_imageFile).image
          : snapshot.hasData
            ? FileImage(snapshot.data!)
            : AssetImage(_imageUrl),
      );
    } else {
      return const Text('Error');
    }
  },
),
Related