Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom route transaction error in flutter

I was trying to implement custom animations for a screen but I'm encountering an error.

The error is:

The getter 'isInitialRoute' isn't defined for the type 'RouteSettings'.

This is my code...

class MyCustomRoute<T> extends MaterialPageRoute<T> {
      MyCustomRoute({ WidgetBuilder builder, RouteSettings settings })
          : super(builder: builder, settings: settings);

      @override
      Widget buildTransitions(BuildContext context,
          Animation<double> animation,
          Animation<double> secondaryAnimation,
          Widget child) {
        if (settings.isInitialRoute)
          return child;
        // Fades between routes. (If you don't want any animation, 
        // just return child.)
        return new FadeTransition(opacity: animation, child: child);
      }
    }

A help will be appreciated!

Thank-you.

like image 447
Karan Owalekar Avatar asked Nov 29 '22 07:11

Karan Owalekar


2 Answers

The isInitialRoute property in RouteSetting has been deprecated. Read more here.

Instead you can do:

MaterialApp(
  initialRouteName: ‘yourRouteName’,

Or:

onGenerateInitialRoutes: (String initialRouteName) {
    return <Route>[YourRoute()];
  }

Please note that your initialRoute is the first route in Navigator. It refers as /. So, if you want you can do this also: settings.name == '/'.

like image 32
Blasanka Avatar answered Dec 05 '22 02:12

Blasanka


After searching around, I found the solution for this.

So I thought I would answer my own question.

Flutter updated the function ans we just have to use settings.name instead of settings.isInitialRoute

class MyCustomRoute<T> extends MaterialPageRoute<T> {
      MyCustomRoute({ WidgetBuilder builder, RouteSettings settings })
          : super(builder: builder, settings: settings);

      @override
      Widget buildTransitions(BuildContext context,
          Animation<double> animation,
          Animation<double> secondaryAnimation,
          Widget child) {
        if (settings.name == '/') {
             return child;
        // Fades between routes. (If you don't want any animation, 
        // just return child.)
        return new FadeTransition(opacity: animation, child: child);
      }
    }
like image 65
Karan Owalekar Avatar answered Dec 05 '22 00:12

Karan Owalekar