Flutter 中的 'canLaunch' 已被弃用。

9
我已使用url_launcher软件包。
String query = Uri.encodeComponent(Utils.getSelectedStoreAddress());
var appleUrl = 'maps:q=$query';
var googleUrl = 'https://www.google.com/maps/search/?api=1&query=$query';

_launch(appleUrl);
_launch(googleUrl);

Future<void> _launch(String url) async {
await canLaunch(url)
    ? await launch(url)
    : _showSnackBar('could_not_launch_this_app'.tr());
}

一切都没问题,直到我遇到了这个警告:

info: 'canLaunch' is deprecated and shouldn't be used. Use canLaunchUrl instead. 
1个回答

14

我按照以下方式更改了我的代码,一切都正常:

String query = Uri.encodeComponent(Utils.getSelectedStoreAddress());
Uri appleUrl = Uri.parse('maps:q=$query');
Uri googleUrl = Uri.parse('https://www.google.com/maps/search/?api=1&query=$query');


_launch(appleUrl);
_launch(googleUrl);

 Future<void> _launch(Uri url) async {
   await canLaunchUrl(url)
    ? await launchUrl(url)
    : _showSnackBar('could_not_launch_this_app'.tr());
}

7
文本翻译:也可以使用 launchUrlString(url),但因为在 launchUrl(...) 的情况下,插件会尽力使 URL 有效(如文档中所述),所以建议使用 launchUrl(...) - Rohan Taneja
在 launchUrl 中的 forceSafariVC 怎么样? - cwhisperer
1
这段代码运行良好。 - Snow Star

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接