Flutter URL launcher Google Maps crashes App

1,447

Solution 1

Issue solved.

I had in my club[index].stadt, which is a list of cities, a city with umlauts as "ä,ö,ü".

By adding final String clubCity = clubs[index].stadt.toString().replaceAll("ü", "ue"); it solved the issue. Because, whenever a city with a umlaut was called within the url, it crashed the app!

Holy cow, that took me ages!

Solution 2

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'

Solution 3

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';
    }
}
Share:
1,447
MuTe33
Author by

MuTe33

Updated on December 16, 2022

Comments

  • MuTe33
    MuTe33 over 1 year

    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)