'How do I save uploaded images in Flutter?
After uploading and saving the image in a container using ImagePicker, if I switch between pages the image will disappear. How do I get it to 'stick'?
This is the code:
class Gallery extends StatefulWidget {
const Gallery({Key? key}) : super(key: key);
@override
State<Gallery> createState() => _GalleryState();
}
class _GalleryState extends State<Gallery> {
File? image;
void getImage({required ImageSource source}) async {
final file = await ImagePicker().pickImage(source: source);
if (file?.path != null) {
setState(() {
image = File(file!.path);
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
iconTheme: IconThemeData(color: Colors.black),
elevation: 0,
backgroundColor: Colors.transparent,
title: Text(
'Lookbook',
style: TextStyle(color: Colors.black, fontWeight: FontWeight.bold),
),
),
body: Row(
children: [
if (image != null)
Container(
width: 200,
height: 200,
decoration: BoxDecoration(
image: DecorationImage(image: FileImage(image!))),
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
