参数类型'AssetGenImage'不能分配给参数类型"图像提供程序"



我正在使用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();
}``

答案在flatter_gen的README.md中。请参阅本节:

使用示例
FlutterGen在资产支持Flutter的情况下生成Image类图像格式。

assets/images/chip.jpg的示例结果:

Assets.images.chip是AssetImage类的一个实现
Assets.images.chip.image(…(返回image类
Assets.images.cchip.provider(…(返回ImageProvider类。   <---此处回答
Assets.images.chip.path只返回路径字符串
Assets.images.chip.values只是返回值列表。

因此,在OP的代码中,Assets.images.logo.provider()应该在不必编辑生成的代码的情况下解决问题(绝对不推荐(。

感觉有点笨重,我知道。

只要用作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;
}

相关内容

最新更新