Home > Mobile >  How to put code with if in a separate method?
How to put code with if in a separate method?

Time:07-14

I'm using app navigation but I'm facing a problem that I can't put the if (initialLink != null)... code in a separate method so that I can then duplicate this method in different parts of the code, can you tell me how can I put this code in a separate method?

class AppRouter {
  final BuildContext context;
  AppRouter(this.context);

  RouteMap buildRouteMap(PendingDynamicLinkData? initialLink) {
    return RouteMap(
      onUnknownRoute: (route) => const Redirect('/'),
      routes: {
        '/': (route) {
          bool loggedIn =
              Provider.of<AppState>(context, listen: false).isLoggedIn;

          if (initialLink != null) {
            String code;
            final Uri deepLink = initialLink.link;
            final String deeplinkString = deepLink.toString();
            code = deepLink.queryParameters['code'] ?? 'No code';
            var start = deeplinkString.lastIndexOf('/')   1;
            var end = deeplinkString.indexOf('?');
            var extract = deeplinkString.substring(start, end);
            if (extract == 'password-reset') {
              return Redirect(
                '/login/forgot-password',
                queryParameters: {
                  'isSendEmail': 'true',
                  'code': code,
                },
              );
            } 
          }
   
          return const MaterialPage(
            child: PhoneNumberPage(),
          );
        },

        '/map': (route) {
          bool loggedIn =
              Provider.of<AppState>(context, listen: false).isLoggedIn;
          if (loggedIn) {
            return const Redirect('/home');
          }

          return const MaterialPage(
            child: MapPage(
              isUserAuth: false,
            ),
          );
        },

code to be placed in a separate function

if (initialLink != null) {
                String code;
                final Uri deepLink = initialLink.link;
                final String deeplinkString = deepLink.toString();
                code = deepLink.queryParameters['code'] ?? 'No code';
                var start = deeplinkString.lastIndexOf('/')   1;
                var end = deeplinkString.indexOf('?');
                var extract = deeplinkString.substring(start, end);
                if (extract == 'password-reset') {
                  return Redirect(
                    '/login/forgot-password',
                    queryParameters: {
                      'isSendEmail': 'true',
                      'code': code,
                    },
                  );
                } 
              }

CodePudding user response:

try this.

void commonFunction(){
  if (initialLink != null) {
    String code;
    final Uri deepLink = initialLink.link;
    final String deeplinkString = deepLink.toString();
    code = deepLink.queryParameters['code'] ?? 'No code';
    var start = deeplinkString.lastIndexOf('/')   1;
    var end = deeplinkString.indexOf('?');
    var extract = deeplinkString.substring(start, end);
    if (extract == 'password-reset') {
      return Redirect(
        '/login/forgot-password',
        queryParameters: {
          'isSendEmail': 'true',
          'code': code,
        },
      );
    }
  }
}

then call commonFunction() every where you need.

CodePudding user response:

You use initialLink as the judgement of if..else condition. When you extract them you need to provide that parameter. Otherwise the code cannot find initialLink.


void commonFunction(PendingDynamicLinkData? initialLink){
  if (initialLink != null) {
    String code;
    final Uri deepLink = initialLink.link;
    final String deeplinkString = deepLink.toString();
    code = deepLink.queryParameters['code'] ?? 'No code';
    var start = deeplinkString.lastIndexOf('/')   1;
    var end = deeplinkString.indexOf('?');
    var extract = deeplinkString.substring(start, end);
    if (extract == 'password-reset') {
      return Redirect(
        '/login/forgot-password',
        queryParameters: {
          'isSendEmail': 'true',
          'code': code,
        },
      );
    }
  }
}
  • Related