【问题标题】:Flutter URL launcher Google Maps crashes AppFlutter URL 启动器 Google Maps 崩溃 App
【发布时间】:2020-04-01 21:17:32
【问题描述】:

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

非常感谢任何帮助。

我的用户界面代码:


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)

【问题讨论】:

  • 尝试使用稳定通道,然后运行flutter clean
  • 我不能使用stable频道,因为我至少需要使用Dart SDK 2.6.0版本。我有一些依赖项需要 Dart SDK 版本。
  • 您好,感谢您发布此问题。我抛出了同样的错误,但不是针对谷歌地图,而是针对电话号码。 canLaunch 和 launch 函数的电话号码格式不正确。它也只会在IOS上抛出错误
  • 是的,不幸的是,错误代码并没有真正的帮助。调试每个值有助于弄清楚这一点。

标签: google-maps url flutter dart crash


【解决方案1】:

根据我的经验,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';
    }
}

【讨论】:

  • 已经阅读了这篇文章。那不能解决我的问题。这就是作者实现谷歌地图的方式。我的问题不一样。我需要使用 URL 启动器来启动谷歌地图给我一个方向。我以前用过它,它工作。但现在没有了。不过,感谢您的帮助!
  • 如果您使用了“google.com”,它会起作用还是仍然崩溃?
  • 不幸的是,它仍然崩溃。
  • 在 try catch 语句中包围 launch 并打印错误(如果有)
【解决方案2】:

问题已解决。

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

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

天哪,我花了好长时间!

【讨论】:

【解决方案3】:

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

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

如果航路点名称 ($name) 包含诸如“纽约”之类的空格,launch_url 不会将空格字符转换为 %20。

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

【讨论】:

  • 谢谢,这也有帮助。不幸的是,错误代码中没有任何内容,但问题已解决:)
【解决方案4】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 2021-04-09
    • 1970-01-01
    • 1970-01-01
    • 2020-10-09
    • 2019-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多