Home > Enterprise >  The argument type 'AssetGenImage' can't be assigned to the parameter type 'Image
The argument type 'AssetGenImage' can't be assigned to the parameter type 'Image

Time:08-17

I am using flutter gen package to To initialize an image

    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,
              )
            ],
          ),
        ),
      ),
    );
  }
}

this is the error:

The argument type 'AssetGenImage' can't be assigned to the parameter type 'ImageProvider'

here is my generated file :

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();
      }``

CodePudding user response:

Just use as Assets.logo.image(), it provides image itself.

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

Right part will be generated

/// 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;
}

CodePudding user response:

I understand where the problem is. In the latest version of this package, a series of changes were made in its generated file. The change was that in the new update, the AssetgenImage file no longer inherits from assetImage, which is an ImageProvider.

To solve this problem, just add the following code to the assets.gen.dart file:

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

    final String _assetName;

Here you can see and check the changes that led to this error:

[https://github.com/FlutterGen/flutter_gen/commit/396aee953d1cc37bd07c8556c397821578cdcc62#:~:text=}-,class AssetGenImage extends AssetImage {,class AssetGenImage {,-const AssetGenImage(][1]

  • Related