Home > database >  Why can I not navigate to URL in Flutter when variable is used?
Why can I not navigate to URL in Flutter when variable is used?

Time:01-24

I have two almost identical functions:

  void _launchGoogleMapsURL() async {
    var GoogleMapsLongLat = widget.place!.location!.LongLatforGoogleMaps()!;
    var url ="https://maps.google.com?q=${GoogleMapsLongLat}"; // DOES NOT WORK
    if (await canLaunch(url)) {
      await launch(url);
    }
  }

And:

  void _actionViewMoreArticles() async {
    var url = "https://mywebsitename.com/plainurl"; // WORKS!
    if (await canLaunch(url)) {
      await launch(url);
    }
  }

And the button:

                CupertinoButton(
                      padding: EdgeInsets.all(0),
                      child: Row(
                        children: <Widget>[
                          Padding(
                            padding: EdgeInsets.only(right: 0, left: 20),
                            child: Text(
                              Localized.of(context)!.trans(LocalizedKey.navigate) ?? "",
                              style: TextStyle(
                                  fontFamily: TraloFont,
                                  fontSize: 16,
                                  color: Colors.white),
                            ),
                          ),
                        ],
                      ),
                      onPressed: _launchGoogleMapsURL,
                    ),

So, if I try to open Google Maps with the long/lat, it does not work (Xcode does not give me any error when tested on my real device), but when I use a 'plain URL' like shown in the example, it does work.

Lastly, here is how the function looks like:

extension longLatforGoogle on String { 
  LongLatforGoogleMaps (){
    var latAndLng = this.split(",").map((text) => double.tryParse(text));
    if (latAndLng.length == 2) {
      return latAndLng;
    }
    return null;
  }
}

What am I doing wrong?

Edit:

This is what the console says:

component name for https://maps.google.com?q=(52.366722, 4.889386) is null

I get the same error with the 'plain URL', but that one actually opens the page and it works.

If I print GoogleMapsLongLat, I get:

(52.366722, 4.889386)

If I only use await launch(url) in Xcode, it gives me the following error:

terminating app due to uncaught exception 'nsinvalidargumentexception' 

and as reason: the specified URL has an unsupported scheme. Only HTTP and HTTP urls are supported.

But that's weird, because I am using HTTPS as you can see.

CodePudding user response:

Fixed it myself. Thanks for the nudge.

I did this:

var NoWhiteSpaceURL = Googleurl.replaceAll(' ', '');
if (await canLaunch(NoWhiteSpaceURL)) {
  await launch(NoWhiteSpaceURL);
}

The problem occurs (apparently), when there is a whitespace in the URL. That is why it is giving those errors. Removing them has solved the issue for me.

  •  Tags:  
  • Related