开发者问题收集

错误:无法将参数类型“void Function(ImageInfo, bool)”分配给参数类型“ImageStreamListener”

2019-07-09
12040

升级 flutter run 后,应用程序无法运行。我升级是因为 ImageStreamListener

我尝试更改不同的渠道 [master、stable、dev],但不起作用。

我在 Flutter 中发现了 这个 问题,但它没有帮助。

这是我的 flutter doctor

[✓] Flutter (Channel unknown, v1.7.11, on Mac OS X 10.14.5 18F132, locale en-PE)
[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
[✓] Xcode - develop for iOS and macOS (Xcode 10.2.1)
[✓] iOS tools - develop for iOS devices
[✓] Chrome - develop for the web
[✓] Android Studio (version 3.4)
[!] IntelliJ IDEA Ultimate Edition (version 2019.1)
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
[✓] VS Code (version 1.36.0)
[✓] Connected device (3 available)

以及错误消息

Compiler message:                                                       
file:///Users/enzoftware/Development/flutter/.pub-cache/hosted/pub.dartlang.org/palette_generator-0.1.1/lib/palette_generator.dart:188:29: Error: The argument type 'void Function(ImageInfo, bool)' can't be assigned to the parameter type 'ImageStreamListener'.
 - 'ImageInfo' is from 'package:flutter/src/painting/image_stream.dart' ('file:///Users/enzoftware/Development/flutter/packages/flutter/lib/src/painting/image_stream.dart').
 - 'ImageStreamListener' is from 'package:flutter/src/painting/image_stream.dart' ('file:///Users/enzoftware/Development/flutter/packages/flutter/lib/src/painting/image_stream.dart').
Try changing the type of the parameter, or casting the argument to 'ImageStreamListener'.
      stream.removeListener(imageListener);                             
                            ^                                           
file:///Users/enzoftware/Development/flutter/.pub-cache/hosted/pub.dartlang.org/palette_generator-0.1.1/lib/palette_generator.dart:194:31: Error: The argument type 'void Function(ImageInfo, bool)' can't be assigned to the parameter type 'ImageStreamListener'.
 - 'ImageInfo' is from 'package:flutter/src/painting/image_stream.dart' ('file:///Users/enzoftware/Development/flutter/packages/flutter/lib/src/painting/image_stream.dart').
 - 'ImageStreamListener' is from 'package:flutter/src/painting/image_stream.dart' ('file:///Users/enzoftware/Development/flutter/packages/flutter/lib/src/painting/image_stream.dart').
Try changing the type of the parameter, or casting the argument to 'ImageStreamListener'.
        stream.removeListener(imageListener);                           
                              ^                                         
file:///Users/enzoftware/Development/flutter/.pub-cache/hosted/pub.dartlang.org/palette_generator-0.1.1/lib/palette_generator.dart:201:24: Error: The argument type 'void Function(ImageInfo, bool)' can't be assigned to the parameter type 'ImageStreamListener'.
 - 'ImageInfo' is from 'package:flutter/src/painting/image_stream.dart' ('file:///Users/enzoftware/Development/flutter/packages/flutter/lib/src/painting/image_stream.dart').
 - 'ImageStreamListener' is from 'package:flutter/src/painting/image_stream.dart' ('file:///Users/enzoftware/Development/flutter/packages/flutter/lib/src/painting/image_stream.dart').
Try changing the type of the parameter, or casting the argument to 'ImageStreamListener'.
    stream.addListener(imageListener);                                  
                       ^                                                
Compiler failed on /Users/enzoftware/Projects/Wibo/wibo-flutter-mobile-user/lib/main.dart

FAILURE: Build failed with an exception.                                

* Where:                                                                
Script '/Users/enzoftware/Development/flutter/packages/flutter_tools/gradle/flutter.gradle' line: 652

* What went wrong:                                                      
Execution failed for task ':app:compileflutterBuildDebugArm'.           
> Process 'command '/Users/enzoftware/Development/flutter/bin/flutter'' finished with non-zero exit value 1

* Try:                                                                  
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org                              

BUILD FAILED in 29s                                                     
Running Gradle task 'assembleDebug'...                                  
Running Gradle task 'assembleDebug'... Done                        29.7s
Gradle task assembleDebug failed with exit code 1

提前致谢。

3个回答

该 API 最近发生了重大变化。

以下是旧用法与新用法的示例

    // get the width, height
    Image image = new Image.file(myImageFile);
    Completer<ImageInfo> completer = Completer();

    // Old API
//    image.image
//        .resolve(new ImageConfiguration())
//        .addListener((ImageInfo info, bool _) {
//      completer.complete(info);
//    });

    // New API
    image.image
        .resolve(new ImageConfiguration())
        .addListener(ImageStreamListener((ImageInfo info, bool _) {
      completer.complete(info);
    }));

    // wait for ImageInfo to finish
    ImageInfo imageInfo = await completer.future;
aaronvargas
2019-07-09

我为此挣扎了几天,然后,作为一个实验,从 zoomable_image 复制了源代码并创建了一个本地文件 zoomable.dart,并编辑了有问题的行:

_imageStream.addListener(_handleImageLoaded);
_imageStream.removeListener(_handleImageLoaded);

_imageStream.addListener(ImageStreamListener(_handleImageLoaded));
_imageStream.removeListener(ImageStreamListener(_handleImageLoaded));

作为 git 新手,我觉得自己没有能力检查 git 文件并修复它。所有者不应该这样做吗?

Scott Anthony
2019-08-02

我从以下链接获得了解决方案:

https://github.com/renefloor/flutter_cached_network_image/issues/197

this._networkImage.image.resolve(ImageConfiguration()).addListener(
            ImageStreamListener((ImageInfo image, bool synchronousCall) {
          if (mounted)
            setState(() => this._imageDownloadState = ImageDownloadState.Done);
        }));

而那些使用 firebase_storage_image.dart 文件的人大多会出现这个问题。上述代码可以解决这个问题。

https://gist.github.com/jeroen-meijer/f0996dd35065c17ac79eeb3938bc89e4

2019-10-18