dnfield / flutter_svg

SVG parsing, rendering, and widget library for Flutter
MIT License
1.68k stars 459 forks source link

The argument type 'Clip?' can't be assigned to the parameter type 'Clip' because 'Clip?' is nullable and 'Clip' isn't. #887

Closed Petri-Oosthuizen closed 1 year ago

Petri-Oosthuizen commented 1 year ago

Getting the following error after upgrading to Flutter 3.7.7:

lib/core/gen/assets.gen.dart:253:21: Error: The argument type 'Clip?' can't be assigned to the parameter type 'Clip' because 'Clip?' is nullable and 'Clip' isn't.

image

Flutter 3.7.7 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 2ad6cd72c0 (8 days ago) • 2023-03-08 09:41:59 -0800
Engine • revision 1837b5be5f
Tools • Dart 2.19.4 • DevTools 2.20.1
#pubspec.yaml
...
flutter_svg: ^2.0.2
dnfield commented 1 year ago

You probably are actually using flutter_svg 2.0.3, which made clipBehavior non-null. Your generated assets will need to specify a clip behavior or not specify null for it.

dnfield commented 1 year ago

That said, I think it's probably fine to make clipBehavior nullable again if it's really disruptive... Let me knwo.

Petri-Oosthuizen commented 1 year ago

You are correct, I was indeed using flutter_svg >= 2.0.3, pinning it to 2.0.2 fixed the problem.

The error comes from within the generated file, assets.gen.dart, however. Setting the clipBehavior in SvgPicture.asset does unfortunately not solve the problem and I'm not setting clipBehavior to null anywhere.

Here's a snippet from the generated file with the error:

class SvgGenImage {
  const SvgGenImage(this._assetName);

  final String _assetName;

  SvgPicture svg({
    Key? key,
    bool matchTextDirection = false,
    AssetBundle? bundle,
    String? package,
    double? width,
    double? height,
    BoxFit fit = BoxFit.contain,
    AlignmentGeometry alignment = Alignment.center,
    bool allowDrawingOutsideViewBox = false,
    WidgetBuilder? placeholderBuilder,
    String? semanticsLabel,
    bool excludeFromSemantics = false,
    SvgTheme theme = const SvgTheme(),
    ColorFilter? colorFilter,
    @deprecated Color? color,
    @deprecated BlendMode colorBlendMode = BlendMode.srcIn,
    @deprecated Clip? clipBehavior,
    @deprecated bool cacheColorFilter = false,
  }) {
    return SvgPicture.asset(
      _assetName,
      key: key,
      matchTextDirection: matchTextDirection,
      bundle: bundle,
      package: package,
      width: width,
      height: height,
      fit: fit,
      alignment: alignment,
      allowDrawingOutsideViewBox: allowDrawingOutsideViewBox,
      placeholderBuilder: placeholderBuilder,
      semanticsLabel: semanticsLabel,
      excludeFromSemantics: excludeFromSemantics,
      theme: theme,
      colorFilter: colorFilter,
      color: color,
      colorBlendMode: colorBlendMode,
      clipBehavior: clipBehavior, // Error: The argument type 'Clip?' can't be assigned to the parameter type 'Clip' because 'Clip?' is nullable and 'Clip' isn't.
      cacheColorFilter: cacheColorFilter,
    );
  }
dnfield commented 1 year ago

Either change this line:

clipBehavior: clipBehavior, // Error: The argument type 'Clip?' can't be assigned to the parameter type 'Clip' because 'Clip?' is nullable and 'Clip' isn't.

to

clipBehavior: clipBehavior ?? Clip.hardEdge

or this line:

 @deprecated Clip? clipBehavior,

to

Clip clipBehavior = Clip.hardEdge

This is compatible with both 2.0.2 and 2.0.3.

Petri-Oosthuizen commented 1 year ago

I did not write this code. It was generated when I ran

flutter pub run build_runner build --delete-conflicting-outputs 

Edit: I realised I should direct this problem towards flutter_gen_runner's flutter_svg integration.