Flutter URL 启动器 Google 地图使应用程序崩溃

Flutter URL launcher Google Maps crashes App

几天前,Google 地图和 Apple 地图都完美启动。我将 Flutter 更新到最新版本,同时在主频道更新到 1.13.1。我假设更新后它停止工作。所以我将频道更改为测试版并进行了尝试,但仍然无法正常工作。尝试启动 Google 地图或 Apple 地图时,IOS 应用程序崩溃,我既不理解错误,也没有在互联网上找到任何东西。

非常感谢任何帮助。

我的UI代码:


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 输出:

[✓] 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!

点击 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)

根据我的经验,onTap 不能很好地与 async 关键字配合使用。我会将逻辑提取到另一种方法中。 build 方法甚至不应该包含逻辑,它们应该只用于 UI 因为它们应该比 运行 便宜并且可以出于任何原因随时调用。

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';
    }
}

问题已解决。

我的 club[index].stadt 是一个城市列表,其中一个城市的变音符号为“ä,ö,ü”。

通过添加 final String clubCity = clubs[index].stadt.toString().replaceAll("ü", "ue"); 它解决了这个问题。因为,每当在 url 中调用带有变音符号的城市时,应用程序就会崩溃!

天哪,这让我花了很多时间!

我有同样的错误。 我的问题是我使用的 url 包含航路点的名称。

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

如果航路点名称 ($name) 包含 space,例如 'New York' launch_url 不会将 space 字符转换为 %20。

我使用 name.toString().replaceAll(' ', "%20") 修复了它。 完整 url 示例: https://maps.apple.com/?q=' + name.toString().replaceAll(' ', "%20") + '&ll=$lat,$lon'

我有完全相同的症状,但我的问题竟然出在 URL 的 &waypoints= 部分。显然 android 能够消化以下 URL (示例):

https://www.google.com/maps/dir/?api=1&origin=1.11,-0.123&destination=2.22,-0.456&waypoints=1.11,-0.123|1.67,-0.245|2.22,-0.456&travelmode=driving&dir_action=navigate

而对于 ios 版本,我必须明确地将 waypoints 中的 | 符号转换为 %7C,所以它看起来像这样:

https://www.google.com/maps/dir/?api=1&origin=1.11,-0.123&destination=2.22,-0.456&waypoints=1.11,-0.123%7C1.67,-0.245%7C2.22,-0.456&travelmode=driving&dir_action=navigate