Animatable<Color> TweenSequence throwing error in Null Safety

Viewed 834

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!

2 Answers

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

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

Related