3
votes

I can't seem to figure out why I keep getting Specify type annotations. dart(always_specify_types) warning under PageRouteBuilder in this function here. I just started using Flutter/Dart and I'm really liking it!

I've tried converting it into a variable and annotating it as a Route but that still did not work out.

Here is the code. Note that it's PageRouteBuilder that's giving me the type annotation warning.

Navigator.of(context).push(
  PageRouteBuilder(
    pageBuilder: (BuildContext context, _, __) {
      return DashboardPage();
    },
    transitionsBuilder: (_, Animation<double> animation, __, Widget child) {
      return FadeTransition(
        opacity: animation,
        child: child,
      );
    },
  ),
);
1
It's working fine for me . - Mazin Ibrahim
Can you show the screenshot of that? - CopsOnRoad

1 Answers

2
votes

PageRouteBuilder is a generic class.

You are getting that hint from the linter because you did not specify the parametric type and the type system has to infer it.

Just add the type of your pushed class (e.g. DashboardPage) with the < > notation in your PageRouteBuilder constructor.

Navigator.of(context).push(
  PageRouteBuilder<DashboardPage>( // <--- parametric type here
    pageBuilder: (BuildContext context, _, __) {
      return DashboardPage();
    },
    transitionsBuilder: (_, Animation<double> animation, __, Widget child) {
      return FadeTransition(
        opacity: animation,
        child: child,
      );
    },
  ),
);

This should get rid of the warning.