How to open a web browser (URL) from Flutter code?

Hello Guys. Many times we need to open some hyperlinks in the flutter project. So in this tutorial, we are going to learn How to open a web browser (URL) from my Flutter code?

Without Wasting Your Time Lets Start This Article.

How to open a web browser (URL) from Flutter code?

open a web browser url in flutter, open a web browser flutter, web browser url flutter, open a web browser (URL) from Flutter code

Time needed: 3 minutes.

Follow All Step To Open a web Browser in flutter

  1. Install Flutter Plugin

    All of we need is to install url_launcher package in our project. Define below line in your pubspec.yaml file.

    url_launcher: ^5.4.2

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

  2. Get dependencies by flutter pub get

    Get dependencies by flutter pub get

  3. Import it

    Now in your Dart code, you can use:
    import 'package:url_launcher/url_launcher.dart';
    How do I open a web browser (URL) from my Flutter code?

  4. Now We are ready to use this plugin

    Follow Below Code to open URL in browser

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

  5. That’s It

    Now You can call this method to open your browser.

Here is my main.dart full source code for batter understanding.

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';
  }
}

So Guys That’s It For Todays tutorial ? tutorial. Hope you like our tutorial. Comment below Your thoughts and your queries. And Also Comment on your suggestion here.

Also Read

Milan Dhameliya is an Information Technology Engineer. By profession, he is a Full Stack developer with knowledge of multiple back-end platforms (e.g., PHP, Node.js, Python) and frontend Flutter frameworks.

Leave a Comment