I am writing my first Flutter app. The app allows the user to take multiple images (from 1 to 50+), and displays each image on the screen all at once using the ListView.
The issue I am having is, the app crashes after roughly 10/12 pictures on the Samsung SM A520F, am guessing this is due to the fact that this is not a very powerful device.
Is there a way I can display the thumbnail of the image instead of loading the full size image?
Error message: I don't actually get any error messages, the app just seems to restart!
Here is my code
import 'package:flutter/material.dart';
import 'package:myapp/utilities/app_constants.dart';
import 'package:image_picker/image_picker.dart';
import 'dart:io';
import 'package:gallery_saver/gallery_saver.dart';
class FormCameraField extends StatefulWidget {
final InputDecoration decorations;
final Map field;
// functions
final Function onSaved;
final Function onFieldSubmitted;
FormCameraField({
this.decorations,
@required this.field,
@required this.onSaved,
@required this.onFieldSubmitted,
});
@override
_FormCameraFieldState createState() => _FormCameraFieldState();
}
class _FormCameraFieldState extends State<FormCameraField> {
List<File> images = [];
Future<void> _takePhoto(ImageSource source) async {
ImagePicker.pickImage(source: source, imageQuality: 90).then(
(File recordedImage) async {
if (recordedImage != null && recordedImage.path != null) {
try {
// store image to device gallery
if (widget.field["StoreCaptureToDevice"] == true) {
GallerySaver.saveImage(recordedImage.path,
albumName: kAppName.replaceAll(" ", "_"));
}
setState(() {
images.add(recordedImage);
});
} catch (e) {
print("ERROR SAVING THE FILE TO GALLERY");
print(e);
}
}
},
);
}
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
Row(
children: <Widget>[
Expanded(
child: MaterialButton(
child: Text("Take Photo"),
onPressed: () async {
await _takePhoto(ImageSource.camera);
},
),
),
Expanded(
child: MaterialButton(
child: Text("Select Photo"),
onPressed: () async {
await _takePhoto(ImageSource.gallery);
},
),
),
],
),
ListView.builder(
shrinkWrap: true,
physics: ClampingScrollPhysics(),
itemCount: images.length,
itemBuilder: (BuildContext context, index) {
return Container(
child: Image.file(
images[index],
),
);
},
)
],
);
}
}