【问题标题】:The argument type 'AssetGenImage' can't be assigned to the parameter type 'ImageProvider'参数类型 \'AssetGenImage\' 不能分配给参数类型 \'ImageProvider\'
【发布时间】:2022-08-19 03:29:58
【问题描述】:

我正在使用flutter gen包来初始化图像

    Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Image(image: Assets.images.logo, height: 64),//error is here
              const SizedBox(
                height: 32,
              ),
              const SpinKitFadingCube(
                color: Solidcolors.primaryColor,
                size: 32.0,
              )
            ],
          ),
        ),
      ),
    );
  }
}

这是错误:

参数类型 \'AssetGenImage\' 不能分配给参数类型 \'ImageProvider\'

这是我生成的文件:

import \'package:flutter/widgets.dart\';

    class $AssetsImagesGen {
    const $AssetsImagesGen();

    /// File path: assets/images/logo.png
     AssetGenImage get logo => const 
     AssetGenImage(\'assets/images/logo.png\');
     }

     class Assets {
      Assets._();

      static const $AssetsImagesGen images = 
      $AssetsImagesGen();
      }``
  • 它是完整的一代课程吗?我认为生成文件时可能有问题,请尝试最新版本

标签: flutter dart


【解决方案1】:

只需用作Assets.logo.image(),它本身就提供图像。

 children: [
       Assets.logo.image(height:64),
  

将生成正确的部分

/// GENERATED CODE - DO NOT MODIFY BY HAND
/// *****************************************************
///  FlutterGen
/// *****************************************************

// coverage:ignore-file
// ignore_for_file: type=lint
// ignore_for_file: directives_ordering,unnecessary_import

import 'package:flutter/widgets.dart';

class Assets {
  Assets._();
  static const AssetGenImage logo = AssetGenImage('assets/logo.jpg');

}

class AssetGenImage {
  const AssetGenImage(this._assetName);

  final String _assetName;

  Image image({
    Key? key,
    AssetBundle? bundle,
    ImageFrameBuilder? frameBuilder,
    ImageErrorWidgetBuilder? errorBuilder,
    String? semanticLabel,
    bool excludeFromSemantics = false,
    double? scale,
    double? width,
    double? height,
    Color? color,
    Animation<double>? opacity,
    BlendMode? colorBlendMode,
    BoxFit? fit,
    AlignmentGeometry alignment = Alignment.center,
    ImageRepeat repeat = ImageRepeat.noRepeat,
    Rect? centerSlice,
    bool matchTextDirection = false,
    bool gaplessPlayback = false,
    bool isAntiAlias = false,
    String? package,
    FilterQuality filterQuality = FilterQuality.low,
    int? cacheWidth,
    int? cacheHeight,
  }) {
    return Image.asset(
      _assetName,
      key: key,
      bundle: bundle,
      frameBuilder: frameBuilder,
      errorBuilder: errorBuilder,
      semanticLabel: semanticLabel,
      excludeFromSemantics: excludeFromSemantics,
      scale: scale,
      width: width,
      height: height,
      color: color,
      opacity: opacity,
      colorBlendMode: colorBlendMode,
      fit: fit,
      alignment: alignment,
      repeat: repeat,
      centerSlice: centerSlice,
      matchTextDirection: matchTextDirection,
      gaplessPlayback: gaplessPlayback,
      isAntiAlias: isAntiAlias,
      package: package,
      filterQuality: filterQuality,
      cacheWidth: cacheWidth,
      cacheHeight: cacheHeight,
    );
  }

  String get path => _assetName;

  String get keyName => _assetName;
}

【讨论】:

  • 它不起作用,它显示此错误:表达式 dosnt 评估为函数,因此无法调用它。
  • 你在使用内栏吗?
  • 是的,我使用内栏
  • 你能包括你生成的文件吗
  • 我把它包括在我的问题中
【解决方案2】:

我明白问题出在哪里。在此软件包的最新版本中,对其生成的文件进行了一系列更改。变化是在新的更新中,AssetgenImage 文件不再继承自作为 ImageProvider 的assetImage。

要解决这个问题,只需将以下代码添加到 assets.gen.dart 文件中:

   class AssetGenImage extends AssetImage {
   const AssetGenImage(this._assetName) : super(_assetName);

    final String _assetName;

在这里,您可以查看并检查导致此错误的更改:

https://github.com/FlutterGen/flutter_gen/commit/396aee953d1cc37bd07c8556c397821578cdcc62#:~:text=%7D-,class%20AssetGenImage%20extends%20AssetImage%20%7B,class%20AssetGenImage%20%7B,-const%20AssetGenImage(

【讨论】:

    猜你喜欢
    • 2021-06-08
    • 2022-08-18
    • 2023-02-10
    • 2021-11-03
    • 2021-11-20
    • 2021-01-05
    • 2018-12-26
    • 2021-06-19
    • 2021-12-13
    相关资源
    最近更新 更多