How to Open URL in External Browser in Flutter

Sometimes, we may want to redirect users to specific links or websites by prompting a browser. In this flutter tutorial let’s check how to open a url in external browser in flutter.

If you are looking to open a url within your app using webview then please check my blog post here.

By default flutter doesn’t provide any option for url schemes. But, no worries, there’s a external package named url launcher which is provided by the official flutter development team.

First add dependencies of url launcher to your pubspec.yaml file.

dependencies:
  flutter:
    sdk: flutter
  url_launcher: 5.7.2

Following is the complete example where url is opened with external browser when the button is pressed.

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

void main() => runApp(App());

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Open Url Example',
      home: OpenUrlExample(),
    );
  }
}

class OpenUrlExample extends StatelessWidget {
  const OpenUrlExample({Key key}) : super(key: key);

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
      title: Text('Open Url flutter example')),
      body: Center(
      child: RaisedButton(
        onPressed: _launchURL,
        child: Text('Show Flutter homepage'),
      ),
    ),
    );
  }
}

I hope this simple flutter tutorial will help you. Stay tuned for more tutorials.

Leave a Comment

Your email address will not be published. Required fields are marked *