Search code examples
flutterflutter-dependenciesflutter-go-routerurl-launcher

Url Launcher stratagy flutter web


i develope app using flutter and used url_launcher and for routing used go_router. while developing app and open url then it's work fine, but when build and upload to my server then it's not working both url_launcher and go_router. i means when. i launch any url after upload server it's open with my server name ex: if i open my social media account using url then

www.example.com/www.instagram.com/myinstaid

and when i hit direct url

www.example.com/dsc

then it's show this page not found. i hope you understand , so please help me about this.

here is code for url launcher

 final Uri weburl = Uri(
  scheme: 'https',
  path: url,
  );
try {
  if (await canLaunchUrl(weburl)) {
    await launchUrl(
        weburl,
        mode: LaunchMode.inAppWebView
    );
  }
}on PlatformException catch (e) {
  debugPrint("PlatformException launchInBrowser : $e");
} on Exception catch (e) {
  debugPrint( "Exception launchInBrowser : $e");
}

go router code:

final GoRouter router = GoRouter(


initialLocation: '/',
  routes: <GoRoute>[
    GoRoute(
      path: '/',
      name: URL_HOME,
      builder: (BuildContext context, GoRouterState state) {
        return const HomeScreen();
      },
      routes: <GoRoute>[
        GoRoute(
          path: URL_DSC,
          name: URL_DSC,
          builder: (BuildContext context, GoRouterState state) {
            return const DSCCertificate();
          },
        ),
        GoRoute(
            path: URL_APPLY_DSC,
            name: URL_APPLY_DSC,
            builder: (BuildContext context, GoRouterState state) {
              return const ApplyDSCScreen();
            }),
        GoRoute(
          path: URL_MSME,
          name: URL_MSME,
          builder: (BuildContext context, GoRouterState state) {
            return const MSMEUdyamScreen();
          },
        ),

    GoRoute(
        path: URL_FASTAG,
        name: URL_FASTAG,
        builder: (BuildContext context, GoRouterState state) {
          return const FastagScreen();
        }),
  ],
),


],
);

thank you :)


Solution

  • finally i resolve this stuff after long days

    firstly Add the url_strategy package to my pubspec.yaml dependencies. In my main.dart file, import the package and call setPathUrlStrategy() before running my app:

    import 'package:url_strategy/url_strategy.dart';
    
    void main() {
      setPathUrlStrategy(); // This removes the hash from the URL
      runApp(MyApp());
    }
    

    i put this code in .htaccess file.(if not exist you can create it) my server main public_html

    RewriteEngine On
    RewriteBase
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ /index.html [L]
    

    this will help me to open deep link url and if i want to open any other domain i just use below code.

    import html import 'dart:html' as html;

    and used it

    html.window.open('https://www.facebook.com/burhanisolution', 'new tab');