How to Launch URL in Flutter

URL launcher

URL launcher is a common thing nowadays in any sort of application. In this article, we will see about how to launch an website url. We are going to use an simple package from pub.dev names url_launcher.

Adding the url_launcher plugin to the dependency

pubspec.yaml file adding dependency

Add the url_launcher plugin in the dependencies in pubspec.yaml file. Then click on the pub get on the top.

importing package

On the main.dart file, import the package for using it in our code.

Create an function for performing the url launcher exception handling

_launchURL() function

In this article, I am naming the function as “_launchURL()”. Since URLs are asynchronous, this function should wait for the url to request and respond back. In the function,
* We should create a constant for declaring the url(website url), in our case we are naming it as url.
*
Now, we should write a simple if-else condition for checking if the url exists or not and respond to us.
* canLaunch(url) is the keyword for sending the request to the website and respond us back to the app to proceed further.
* launch(url) is the keyword to launch the url in our app. If the url does not exist, it should throw an error, so we can throw it easily using throw keyword.
* Now then, that’s all we need to look into it for the function. Now we can easily incorporate it in our app

Note: asynchronous means the app need to request the website and then the website need to respond back to the app. While these process taking place, the app should wait for the respond from the website.

Create a StalessWidget

  • After creating the stateless widget, for simplicity just create a RaisedButton in the body of the scaffold which is inside the materialapp widget.
  • On the onpressed property, give our _launchURl function to execute.
  • That’s it, the url will be launched from our app.
urlLauncher app

Entire code:

You can also find the entire code for the project here: https://github.com/vishnuvichu14/flutterurllauncher

Thank you for reading.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Processing large data files with python multithreading

HTML File Structure: A Learning Journey to Web Development

Least Privilege for AWS Lambda Security

Trie: Part 2

C# Connect To MySQL In Visual Studio 2019

The birth of ID3TagEditor and Mp3ID3Tagger and my journey into the ID3 tag standard

FlutterForce — #Week 77

Logic or analytical communication language, and the importance of emotionless communication…

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vishnuvarshan P

Vishnuvarshan P

More from Medium

#FlutterWithSakshi — Part 2

My Flutter Islamic Calendar App

Flutter With Huawei Account and Ads Kit

Become Full Stack Developer 👌🏻 | Start creating a awesome app for android & iOS