Animatable<Color> TweenSequence throwing error in Null Safety

618

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?>,
  ),
]);
Share:
618
oupoup
Author by

oupoup

Updated on December 01, 2022

Comments

  • oupoup
    oupoup over 1 year

    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!