尝试显示超过 10 张图像时 Flutter 应用程序崩溃

use*_*745 7 camera image flutter

我正在编写我的第一个 Flutter 应用程序。该应用程序允许用户拍摄多张图像(从 1 到 50 多张),并使用 ListView 在屏幕上一次性显示每张图像。

我遇到的问题是,应用程序在三星 SM A520F 上大约 10/12 张图片后崩溃,我猜这是因为这不是一个非常强大的设备。

有没有办法可以显示图像的缩略图而不是加载全尺寸图像?

错误消息:我实际上没有收到任何错误消息,应用程序似乎重新启动了!

这是我的代码

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],
              ),
            );
          },
        )
      ],
    );
  }
}


Run Code Online (Sandbox Code Playgroud)

小智 2

有类似的问题。用较小的(~50kb)图像替换我的图像似乎工作正常,所以我认为你是对的,在功能较弱的设备上加载所有这些图像似乎是问题所在。pub.dev上有一个image包可以解决这个问题。我正在使用 Firebase,所以我会更倾向于他们新的调整大小图像扩展。祝你好运,让我们知道它是否有效