错误:无法将参数类型“void Function(ImageInfo, bool)”分配给参数类型“ImageStreamListener”
Posted
技术标签:
【中文标题】错误:无法将参数类型“void Function(ImageInfo, bool)”分配给参数类型“ImageStreamListener”【英文标题】:Error: The argument type 'void Function(ImageInfo, bool)' can't be assigned to the parameter type 'ImageStreamListener' 【发布时间】:2019-11-19 07:10:59 【问题描述】:升级 flutter run 后,应用程序无法运行。我升级是因为ImageStreamListener
我试图改变不同的渠道 [master, stable, dev] 并且不起作用。
我在 Flutter 中发现了 this 问题,但它没有帮助。
这是我的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
提前致谢。
【问题讨论】:
【参考方案1】:我从下面的链接得到了解决方案:
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
【讨论】:
【参考方案2】:我为此苦苦挣扎了几天,然后,作为一个实验,从 zoomable_image 复制源代码并创建了一个本地文件 zoomable.dart 并编辑了有问题的行:
_imageStream.addListener(_handleImageLoaded);
_imageStream.removeListener(_handleImageLoaded);
到
_imageStream.addListener(ImageStreamListener(_handleImageLoaded));
_imageStream.removeListener(ImageStreamListener(_handleImageLoaded));
我觉得没有能力检查 git 文件并将其修复为 git 新手。楼主不应该这样做吗?
【讨论】:
【参考方案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;
【讨论】:
@Daibaku,看起来 2019 年 7 月 19 日发布了 palette_generator 插件的 0.2.0 版(与问题中的 0.1.1 相比)。所以这可能会更新到新的 API。跨度> 感谢亚伦的评论。实际上,在将 sdk 升级到最新版本后,我遇到了这个问题。我该怎么办? @aaronvargas 我需要在哪里应用这些更改?因为在我的源代码中我从来没有找到那个旧代码。 @HirenVaghela,您正在使用的库可能需要更新。在 OP 中,palette_generator-0.1.1 需要更新到 0.2.0以上是关于错误:无法将参数类型“void Function(ImageInfo, bool)”分配给参数类型“ImageStreamListener”的主要内容,如果未能解决你的问题,请参考以下文章
如何将“Function”类型的参数分配给“void Function()”类型的参数?
参数类型“Function”不能分配给参数类型“void Function(bool)?”
参数类型“Function”不能分配给参数类型“void Function()?”在零安全之后
参数类型“void Function(Map<String, bool>)”不能分配给参数类型“Map<String, bool>”