close

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

what do you mean by load a page in ripple application…

While creating applications you will need to open the site in your App itself, this should be possible utilizing url_launcher.

https://pub.dartlang.org/packages/url_launcher

We should begin… .

Step 1: Add the conditions to your pubspec.yaml under conditions

url_launcher: ^4.0.3

Step 2: Install

You can introduce by tapping on bundles jump (on the upper right-hand corner in case you’re utilizing android studio)

Or on the other hand…

Utilize terminal

$ flutter packages get

Step 3: Import

Presently import this bundle onto your dart code

import 'package:url_launcher/url_launcher.dart’;

Step 4: Create a catch

Here I will make the catch utilizing RaisedButton. In onPressed pass in _launchURL which will be made in the following stage.

RaisedButton(
  onPressed: _launchURL,
  child: Text('open'),
);

Step 5: Create _launchURL work

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

Step 6: Run the App

Extra Information:

In the Above referenced manner the site page is opened in you’re program than inside your application.

To make it open inside the App use forceWebView: and set it to valid in dispatch strategy.

like this…

_launchURL() async {
  const url = "https://google.com";
  if (await canLaunch(url)) {
    await launch(url, forceWebView: true); //forceWebView
  } else {
    throw 'Could not launch $url';
  }
}

Complete Example:

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
// url launcher
void main() {
  runApp(MaterialApp(
    home: Scaffold(
      body: Center(
        child: RaisedButton(
          onPressed: _launchURL,
          child: Text('open'),
        ),
      ),
    ),
  ));
}
_launchURL() async {
  const url = "https://google.com";
  if (await canLaunch(url)) {
    await launch(url);
  } else {
    throw 'Could not launch $url';
  }
}

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also Read

Leave a Comment