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

FlutterDartUrlBrowserWebbrowser Control

Flutter Problem Overview


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?

Flutter Solutions


Solution 1 - Flutter

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
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.

Solution 2 - Flutter

If you target sdk 30 or above canLaunch will return false by default due to package visibility changes: https://developer.android.com/training/basics/intents/package-visibility

in the androidManifest.xml you'll need to add the following

<queries>
    <intent>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="https" />
    </intent>
</queries>

Then the following should word

const url = "https://flutter.io";
if (await canLaunch(url)){
  await launch(url);
}
else {
  // can't launch url
}

Solution 3 - Flutter

For Flutter:

As described above by Günter Zöchbauer

For Flutter Web:

import 'dart:html' as html;

Then use:

html.window.open(url, name);

Make sure that you run flutter clean if the import doesn't resolve.

Solution 4 - Flutter

For those who wants to implement LAUNCH BROWSER AND EXIT APP by using url_launcher. Remember to use (forceSafariVC: false) to open the url in default browser of the phone. Otherwise, the launched browser exit along with your APP.

await launch(URL, forceSafariVC: false);

Solution 5 - Flutter

The best way is to use url_launcher package .

Add url_launcher as a dependency in your pubspec.yaml file.

dependencies:
  url_launcher: ^5.7.10

An example of how to use it :

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

void main() {
  runApp(
    MaterialApp(
        home: Scaffold(
      appBar: AppBar(title: Text('Flutter is beautiful'),),
      body: Center(
        child: RaisedButton(
          onPressed: _launchURL,
          child: Text('Show Flutter homepage'),
        ),
      ),
    )),
  );
}

_launchURL() async {
  const url = 'https://flutter.dev';
  if (await canLaunch(url)) {
    await launch(url);
  } else {
    throw 'Could not launch $url';
  }
}

Output : enter image description here > The launch method takes a string argument containing a URL . > By default, Android opens up a browser when handling URLs. You can > pass forceWebView: true parameter to tell the plugin to open a WebView > instead. If you do this for a URL of a page containing JavaScript, > make sure to pass in enableJavaScript: true, or else the launch method > will not work properly. On iOS, the default behavior is to open all > web URLs within the app. Everything else is redirected to the app > handler.

Solution 6 - Flutter

If you want to use url_launcher than please use it in this form

environment:
  sdk: ">=2.1.0 <3.0.0"

dependencies:
  url_launcher: ^5.0.2
  flutter:
    sdk: flutter

This answer is also for absolute beginners: They are thinking behind the flutter sdk. No that was a failure. The packages were extras and not in the flutter Sdk. These were secondary packages (single small framework helpers).

Solution 7 - Flutter

After some search, this issue can be solved via instructions listed here: https://groups.google.com/forum/#!topic/flutter-dev/J3ujgdOuG98

The above UrlLauncher is no longer usable.

Solution 8 - Flutter

Use url_launcher package . It will do this for you

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSeth LaddView Question on Stackoverflow
Solution 1 - FlutterGünter ZöchbauerView Answer on Stackoverflow
Solution 2 - FlutterAndrewView Answer on Stackoverflow
Solution 3 - FlutterRohan TanejaView Answer on Stackoverflow
Solution 4 - FlutterPai-Hsiang HuangView Answer on Stackoverflow
Solution 5 - FlutterKab AgoudaView Answer on Stackoverflow
Solution 6 - Flutteredgar wahlView Answer on Stackoverflow
Solution 7 - FlutterTaylorRView Answer on Stackoverflow
Solution 8 - Flutteruser11644496View Answer on Stackoverflow