简体   繁体   English

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

[英]Flutter app crashing when trying to display more than 10 images

I am writing my first Flutter app.我正在编写我的第一个 Flutter 应用程序。 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.该应用程序允许用户拍摄多张图像(从 1 到 50+),并使用 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.我遇到的问题是,应用程序在三星 SM A520F 上拍摄大约 10/12 张图片后崩溃,我猜这是因为这不是一个非常强大的设备。

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


Having a similiar problem.有类似的问题。 Replacing my images with smaller (~50kb) ones seems to be working fine so i think you are right, loading all those images on less powerfull devices seems to be the problem.用较小(~50kb)的图像替换我的图像似乎工作正常,所以我认为你是对的,在功能较弱的设备上加载所有这些图像似乎是问题所在。 There is an image package on pub.dev that should do the trick. pub.dev 上有一个image package应该可以解决问题。 I am using Firebase so i will lean more towards their new resize image extension.我正在使用 Firebase 所以我会更倾向于他们新的调整大小图像扩展。 Goodluck and let us know if it works祝你好运,让我们知道它是否有效

I have the same problem.我也有同样的问题。 It's actually a bug in flutter.这实际上是 flutter 中的一个错误。 They are currently working to fix the bug in the next stable releases.他们目前正在努力修复下一个稳定版本中的错误。

I faced the same problem.我遇到了同样的问题。

This is because some images cause the crash of Flutter engine.这是因为某些图像导致 Flutter 引擎崩溃。 The final issue is here https://github.com/flutter/flutter/issues/73767最后一个问题在这里https://github.com/flutter/flutter/issues/73767

The example image that always causes crash on ios is here https://github.com/flutter/flutter/issues/73932总是导致 ios 崩溃的示例图像在这里https://github.com/flutter/flutter/issues/73932

So, waiting for the Flutter team fixes the bug.因此,等待 Flutter 团队修复该错误。

I created thumbnails, this worked for me.我创建了缩略图,这对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM