flutterdarturlbrowserwebbrowser-control

How do I open a web browser (URL) from my Flutter code?


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?


Solution

  • TL;DR

    This is now implemented as Plugin

     final Uri url = Uri.parse('https://flutter.dev');
     if (!await launchUrl(url)) {
          throw Exception('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 {
       final Uri url = Uri.parse('https://flutter.dev');
       if (!await launchUrl(url)) {
            throw Exception('Could not launch $_url');
        }
    }
    

    In pubspec.yaml

    dependencies:
      url_launcher: ^6.1.11
    

    Check out the latest url_launcher package.

    Special Characters:

    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.