Flutter null-safety migration

Viewed 423

When I use the dart migration tool, it suggests that casting for builder of AnimatedBuilder:

Added a cast to an expression (non-downcast)

Is this really necessary?

enter image description here

2 Answers

Not really, not only this but whatever cast that is optional and when you remove it, the compiler goes on, it will work. Just in case something is ambiguous to the compiler, which is not in your case, you should cast explicitly.

You get the suggestion because the signature of builder in AnimatedBuilder class has a child of type nullable Widget i.e. Widget?

TransitionBuilder = Widget Function(BuildContext context, Widget? child);

You can either remove the cast or use

AnimatedBuilder(
  builder: (_, Widget? child) { // <-- Added Widget?
    return Container();
  },
)
Related