2

I just migrated my flutter app in Null Safety. Everything seems to be working well, except the below code:

Animatable<Color> animColorPend = TweenSequence<Color>([
  TweenSequenceItem(
    weight: 1.0,
    tween: ColorTween(
      begin: Colors.purple,
      end: Colors.white,
    ) as Animatable<Color>,
  ),
  TweenSequenceItem(
    weight: 1.0,
    tween: ColorTween(
      begin: Colors.white,
      end: Colors.purple,
    ) as Animatable<Color>,
  ),
]);

The cast (as Animatable) is throwing this error:

type 'ColorTween' is not a subtype of type 'Animatable' in type cast

Previously (and when I run the app with --no-sound-null-safety) I didn't get this error. Could that be a bug coming from something not yet implemented, as it says here (https://flutter.dev/docs/null-safety)

Not all parts of the Flutter SDK support null safety yet, as some parts still need additional work to migrate to null safety.

Or do you think there is something in the code? Everything I tried (removing the cast, initializing a TweenSequence and not the abstract class and so on) will not work. Thanks for the help!

oupoup
  • 75
  • 8
  • 2
    Does this answer your question? [flutter dart error dart(argument\_type\_not\_assignable)](https://stackoverflow.com/questions/67036462/flutter-dart-error-dartargument-type-not-assignable) – linxie Apr 22 '21 at 14:29
  • Yes indeed! This one is what I used and solved the issue. Apparently there is sth going on with Animatable... Thank you very much – oupoup May 12 '21 at 05:58

2 Answers2

2

For some reason that I do not understand you have to make the color nullable.

Try this:

Animatable<Color> animColorPend = TweenSequence([
  TweenSequenceItem(
    weight: 1.0,
    tween: ColorTween(
      begin: Colors.purple,
      end: Colors.white,
    ) as Animatable<Color?>,
  ),
  TweenSequenceItem(
    weight: 1.0,
    tween: ColorTween(
      begin: Colors.white,
      end: Colors.purple,
    ) as Animatable<Color?>,
  ),
]);
Charlie Salts
  • 13,109
  • 7
  • 49
  • 78
Leo
  • 21
  • 3
1

This is an issue with null safety replace

Animatable<Color> animColorPend = TweenSequence<Color>([

with

Animatable<Color?> animColorPend = TweenSequence<Color?>([

as of flutter stable 3.0.2

Mahesh Jamdade
  • 17,235
  • 8
  • 110
  • 131