TL; DR
现在已实现为插件
const url = "https://flutter.io";
if (await canLaunch(url))
await launch(url);
else
throw "Could not launch $url";
完整示例:
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
void main() {
runApp(new Scaffold(
body: new Center(
child: new RaisedButton(
onPressed: _launchURL,
child: new Text('Show Flutter homepage'),
),
),
));
}
_launchURL() async {
const url = 'https://flutter.io';
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
在pubspec.yaml中
dependencies:
url_launcher: ^5.4.2
特殊字符:
如果url
值包含空格或URL现在允许的其他值,请使用
Uri.encodeFull(urlString)
或Uri.encodeComponent(urlString)
,而是传递结果值。