I am building a Flutter app, and I'd like to open a URL into a web browser or browser window (in response to a button tap). How can I do this?
TL;DR
This is now implemented as Plugin
const url = "https://flutter.io";
if (await canLaunch(url))
await launch(url);
else
// can't launch url, there is some error
throw "Could not launch $url";
Full example:
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'; } }
In pubspec.yaml
dependencies:
url_launcher: ^5.7.10
If the url
value contains spaces or other values that are now allowed in URLs, use
Uri.encodeFull(urlString)
or Uri.encodeComponent(urlString)
and pass the resulting value instead.
Note that you might need to either create a new flutter project and copy over your Flutter specific stuff (
lib
,pubspec.yaml
, etc.) or conversely update the platform specific folders in your old project for this to work.Note: Don't forget to add
url_launcher: ^3.0.2
to the pubspec.yamlWhat do you mean with "added to existing apps"? Perhaps your files in
android/
orios/
are outdated. If it works in a new app then compare the files and update them in your project. You can also delete these directories and runflutter create .
and then re-apply manual changes.@hbhakhra Now url_launcher: ^5.0.2 Keep checking.
How to open link for flutter_web where url_launcher plug-in is not available ?