Search code examples
flutterweburl-parameters

passing url parameters to flutter web


I'm working on a flutter project that covers multiple platforms. My app has a payment api, which also has a callback url that sends some url parameters to my result screen. The url for my payment result screen is https://myapp.com/payment-result. I'd like to read the parameters of my call back url, which are: https://myapp.com/payment-result/?status=20&amount=2 This url, on the other hand, always restarts my web application and takes me to the first page of the app.

this is my payment result screen :


class PaymentResult extends StatelessWidget {
  static const routeName = '/payment-result';

  @override
  Widget build(BuildContext context) {
    final args = ModalRoute.of(context)!.settings.arguments as Map;
    bool shouldPop = true;
    return WillPopScope(
      onWillPop: () async {
        Navigator.of(context).popAndPushNamed('tab-screen');
        return shouldPop;
      },
      child: Scaffold(
        body:  kIsWeb ? WebWidget(args:args): MobileWidget(args: args),
      ),
    );
  }
}

and I managing my routs on main widget like this :


   routes: {
                    SelectPlan.routName: (ctx) => SelectPlan(),
                    MealDetails.routeName: (ctx) => MealDetails(),
                    AuthScreen.routeName: (ctx) => AuthScreen(),
                    PaymentResult.routeName: (ctx) => DailyDetails(),
                    SelectSub.routeName: (ctx) => SelectSub(),
                    OrderScreen.routName: (ctx) => OrderScreen(),


Solution

  • You can use go_router package from https://pub.dev.

    it is made by flutter team for this kind of routing.

    1. Basic set up
    class App extends StatelessWidget {
      App({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) => MaterialApp.router(
            routeInformationParser: _router.routeInformationParser,
            routerDelegate: _router.routerDelegate,
            title: 'GoRouter Example',
          );
    
      final GoRouter _router = GoRouter(
        routes: <GoRoute>[
          GoRoute(
            path: '/',
            builder: (BuildContext context, GoRouterState state) => const Page1Screen(),
          ),
          GoRoute(
            path: '/page2',
            builder: (BuildContext context, GoRouterState state) => const Page2Screen(),
          ),
        ],
      );
    }
    
    class Page1Screen extends StatelessWidget {...}
    
    class Page2Screen extends StatelessWidget {...}
    
    1. For Parameter pass : https://gorouter.dev/parameters

    official docs: https://gorouter.dev/