在调试模式与发布模式下波动不同的行为

时间:2018-10-06 10:19:10

标签: flutter dart-isolates

我正在为Flutter应用开发这些功能:从图库中加载图像,调整大小并保存。

调整大小部分是CPU密集型操作,因此我遵循建议的here使用隔离的方法来获得更好的用户体验。

当我在调试模式下运行代码时,没有问题,但是当我在发布模式下尝试相同的代码时,我的映像保存在wrong way中。

有人可以帮助我理解我所解释的问题吗?

我仅在以下设备上测试代码:具有Android 7.0的HUAWEI P9 lite VNS-L31

以下是复制问题的代码。这是一个新的flutter项目,其中包含以下文件:

pubspec.yaml中,添加以下部分:

dependencies:
  path_provider: ^0.4.1
  image_picker: ^0.4.10
  image: ^2.0.4
  flutter:
    sdk: flutter

所有代码都在lib/main.dart中:

import 'dart:async';
import 'dart:io';
import 'dart:isolate';

import 'package:flutter/material.dart';
import 'package:image/image.dart';
import 'package:image_picker/image_picker.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart' as p;

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key}) : super(key: key);

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  File _file;

  Future<File> _getImage() async {
    File image = await ImagePicker.pickImage(source: ImageSource.gallery);
    if (image != null) {
      return image;
    }
    return null;
  }

  static decode(DecodeParam param) async {
    var p = await param.file.readAsBytes();
    var image = decodeImage(p);
    var thumbnail = copyResize(image, 120);
    param.sendPort.send(thumbnail);
  }

  void _displayImage() async {
    setState(() {
      _file = null;
    });

    File file = await _getImage();
    ReceivePort receivePort = new ReceivePort();

    await Isolate.spawn(decode, new DecodeParam(file, receivePort.sendPort));

    var image = await receivePort.first;

    Directory tempDir = await getTemporaryDirectory();
    String tempPath = tempDir.path;

    File profilePictureFile =
        File(p.join(tempPath, 'thumbnail' + _counter.toString() + '.png'))
          ..writeAsBytesSync(encodePng(image));

    setState(() {
      _counter++;
      _file = profilePictureFile;
    });
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            _file != null
                ? Container(
                    height: 200.0,
                    width: 200.0,
                    decoration: BoxDecoration(
                        shape: BoxShape.circle,
                        image: DecorationImage(
                            fit: BoxFit.fitWidth, image: FileImage(_file))))
                : Container(),
          ],
        ),
      ),
      floatingActionButton: new FloatingActionButton(
        onPressed: _displayImage,
        child: new Icon(Icons.add),
      ),
    );
  }
}

class DecodeParam {
  final File file;
  final SendPort sendPort;
  DecodeParam(this.file, this.sendPort);
}

flutter doctor -v

[√] Flutter (Channel master, v0.9.7-pre.61, on Microsoft Windows [Versione 10.0.15063], locale it-IT)
    • Flutter version 0.9.7-pre.61 at C:\src\flutter
    • Framework revision 2d81adf74c (2 days ago), 2018-10-05 22:29:37 -0700
    • Engine revision 572fa5646a
    • Dart version 2.1.0-dev.6.0.flutter-c6254163dc

[√] Android toolchain - develop for Android devices (Android SDK 28.0.1)
    • Android SDK at d:\Profiles\alarosa\AppData\Local\Android\sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-28, build-tools 28.0.1
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b02)
    • All Android licenses accepted.

[√] Android Studio (version 3.1)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin version 26.0.1
    • Dart plugin version 173.4700
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b02)

[!] IntelliJ IDEA Community Edition (version 2018.1)
    • IntelliJ at C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.1
    X Flutter plugin not installed; this adds Flutter specific functionality.
    X Dart plugin not installed; this adds Dart specific functionality.
    • For information about installing plugins, see
      https://flutter.io/intellij-setup/#installing-the-plugins

[√] VS Code (version 1.27.2)
    • VS Code at d:\Profiles\alarosa\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 2.19.0

[√] VS Code, 64-bit edition (version 1.27.2)
    • VS Code at C:\Program Files\Microsoft VS Code
    • Flutter extension version 2.19.0

[√] Connected device (1 available)
    • HUAWEI VNS L31 • 4TE0216A14001341 • android-arm64 • Android 7.0 (API 24)

! Doctor found issues in 1 category.

3 个答案:

答案 0 :(得分:2)

按照@GunterZochbauer的建议,我在flutter存储库中打开了an issue

Flutter团队正在努力,正如您可以在github线程上阅读的那样,Jason-Simmons建议以下解决方法。

将发件人更改为:

param.sendPort.send([thumbnail.width, thumbnail.height, thumbnail.data]);

和接收方:

List<dynamic> imageData = await receivePort.first;
var image = new Image.fromBytes(imageData[0], imageData[1], imageData[2]);

答案 1 :(得分:0)

writeAsBytesSync方法具有一个参数flush,默认值为false。

flush的注释:如果[flush]参数设置为true,则写入的数据将在返回之前刷新到文件系统。

将代码编辑到下一个:

File profilePictureFile =
        File(p.join(tempPath, 'thumbnail' + _counter.toString() + '.png'))
          ..writeAsBytesSync(encodePng(image), flush: true);

检查是否有更改。

非隔离具有内存共享。如果隔离,则文件需要读取文件系统,并且文件内容可能尚未写入或只有一半。

答案 2 :(得分:0)

我有类似的东西,但是显示图像,通过移除展开的图像,可以固定行为。