Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter URL launcher Google Maps crashes App

A couple of days ago launching both Google Maps and Apple Maps perfectly fine. I updated Flutter to its newest Version while on master channel to 1.13.1. I assume after updating it stopped working. So I changed channels to beta and tried, but still not working. When trying to launch Google Maps or Apple Maps the IOS app crashes and I neither understand the error nor did I find something on the internet.

Any help is much appreciated.

My UI code:


ListTile(
                 title: Text("Launch Google Maps", textAlign: TextAlign.center),
                  onTap: () async {
                    final String clubName =
                        clubs[index].name.toString().replaceAll(" ", "+");
                    print("Name: $clubName"); // 'Flawless+Club'
                    final String clubCity = clubs[index].stadt;
                    print("Name: $clubCity"); // 'London'
                    final String newGoogleUrl =
                        "https://www.google.com/maps/dir/?api=1&destination=$clubName+$clubCity";

                    if (await canLaunch(newGoogleUrl)) {
                      await launch(newGoogleUrl);
                    } else {
                      throw 'Could not launch $newGoogleUrl';
                    }
                  },
                ),

Flutter doctor -v Ouput:

[✓] Flutter (Channel beta, v1.12.13+hotfix.3, on Mac OS X 10.15.1 19B88, locale en-DE)
    • Flutter version 1.12.13+hotfix.3 at /Library/Flutter
    • Framework revision 57f2df76d7 (2 days ago), 2019-12-05 21:23:21 -0800
    • Engine revision ac9391978e
    • Dart version 2.7.0


[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    • Android SDK at /Users/murat/Library/Android/sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-28, build-tools 28.0.3
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1343-b01)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 11.2.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 11.2.1, Build version 11B500
    • CocoaPods version 1.8.4

[✓] Android Studio (version 3.4)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 35.3.1
    • Dart plugin version 183.6270
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1343-b01)

[✓] VS Code (version 1.40.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.7.0

[✓] Connected device (1 available)

• No issues found!

Error when tapping the ListTile:

Lost connection to device.
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff23c4f02e __exceptionPreprocess + 350
    1   libobjc.A.dylib                     0x00007fff50b97b20 objc_exception_throw + 48
    2   Foundation                          0x00007fff2578c55b -[__NSConcreteURLComponents initWithString:] + 0
    3   CoreServices                        0x00007fff24e970bd -[_LSURLOverride initWithOriginalURL:checkingForAvailableApplications:newsOnly:] + 151
    4   CoreServices                        0x00007fff24e97992 -[_LSURLOverride initWithOriginalURL:newsOnly:] + 25
    5   CoreServices                        0x00007fff24e982ae _ZN14LaunchServices12URLOverridesL20getURLOverrideCommonEP5NSURLb + 399
    6   CoreServices                        0x00007fff24e9810e -[LSApplicationWorkspace(LSURLOverride) URLOverrideForURL:] + 14
    7   UIKitCore<…>
Exited (sigterm)
like image 291
MuTe33 Avatar asked Mar 13 '26 22:03

MuTe33


2 Answers

I had the same error. My problem was that I was using this url which contain a name for the waypoint.

'https://maps.apple.com/?q=' + '$name' + '&ll=$lat,$lon';

If the waypoint name ($name) contains spaces like 'New York' launch_url is not converting the space character to %20.

I fixed it by using name.toString().replaceAll(' ', "%20"). Full url example: https://maps.apple.com/?q=' + name.toString().replaceAll(' ', "%20") + '&ll=$lat,$lon'

like image 160
Nuno Ferreira Avatar answered Mar 15 '26 15:03

Nuno Ferreira


In my experience, onTap doesn't play nicely with the async keyword. I would extract the logic into another method. build Methods shouldn't even contain logic in them, they should be for UI only since they should be cheap to run and can be called at any time for any reason.

onTap: () => myFunction(clubs, index),

// the function
void myFunction(var clubs, int index) async {
    final String clubName = clubs[index].name.toString().replaceAll(" ", "+");
    print("Name: $clubName"); // 'Flawless+Club'
    final String clubCity = clubs[index].stadt;
    print("Name: $clubCity"); // 'London'
    final String newGoogleUrl = "https://www.google.com/maps/dir/?api=1&destination=$clubName+$clubCity";

    if (await canLaunch(newGoogleUrl)) {
        await launch(newGoogleUrl);
    } else {
        throw 'Could not launch $newGoogleUrl';
    }
}
like image 26
Benjamin Avatar answered Mar 15 '26 16:03

Benjamin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!