I'm trying to store image (picked from ImagePicker) in shared preferences, so I can persist the data
This is what I did so far
Here is the code
File? profileImage;
void saveData(String key, String value) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString(key, value);
}
Future pickProfile() async {
final profileImagePicker = await ImagePicker().pickImage(source: ImageSource.gallery);
final directoryPath = await getApplicationDocumentsDirectory();
final path = directoryPath.path;
final imageFile = await File(profileImagePicker!.path).copy('$path/image1.png');
saveData('primaryProfile', imageFile.toString()); // Store it as a string, don't know if this is the right away
setState(() {
profileImage = imageFile;
});
}
void getPrimaryProfile() async {
final prefs = await SharedPreferences.getInstance();
final value = prefs.getString('primaryProfile'); // How can I convert this to File object from String, so I can assign it to file object (profileImage)
}
//To display the image
CircleAvatar(
backgroundImage: profileImage != null ? FileImage(profileImage!) as ImageProvider : const AssetImage('assets/images/profile.png'),
backgroundColor: Colors.black45
),