1

here is the code snippet that I trying to launch url,

onPressed: () async {
              final Uri url = Uri.parse(state.web);
               if ( await canLaunchUrl(url)) {
                   aunchUrl(url);
               }else{
                  printLogs('cannot launch');
               }
          },

state.web is https://www.facebook.com/xxxxxxx/

  • this is comes from API,

and also I've tried below method as well,

onTap: () async {
    final Uri url = Uri(scheme: 'https', host: state.web, path:'');
           if (!await launchUrl(url,
              mode: LaunchMode.externalApplication)) {
                  throw 'Could not launch $url';
              }
 },

second method is working if there is only www.facebook.com, I need to add /xxxxxxx/ complete url. Is there any method to separate this /xxxxxxx/ part and get it separately ?

problem is above both methods are not working. requirement is open the url in browser/related application.

Sasitha Dilshan
  • 111
  • 2
  • 15

1 Answers1

0

firstly, split the url string, after split it can get the url like an array, like below,

[0]: "www.facebook.com" 
[1]: "xxxxxxx"

then we can use the below method to launch the url in browser.

onPressed: () async {
           final String source = https://www.facebook.com/xxxxxxx/;
                  final splitedString = source.split('/');
    
                  final Uri url = Uri(
                    scheme: 'https',
                    host: splitedString[0],
                    path: '/${splitedString[1]}/');
                     if (!await launchUrl(url,
                          mode: LaunchMode.externalApplication)) {
                          throw 'Could not launch $url';
                     }
             },
Sasitha Dilshan
  • 111
  • 2
  • 15