'canLaunch' 在 Flutter 中已弃用

'canLaunch' is deprecated in Flutter

我用过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. 

我如下更改了我的代码,一切正常:

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());
}