How to Open Web URL From Flutter Code?
Open URL follow the instruction as below:
Using the URL Launcher plugin url_launcher
- In pubspec.yaml
dependencies: url_launcher: ^5.4.2
- Import package like below:
import 'package:url_launcher/url_launcher.dart';
To launching a web page, Let’s be an async function and do the following:
launchURL(String url) async { if (await canLaunch(url)) { await launch(url); } else { throw 'Could not launch $url'; } }
If we wanted both iOS and Android to open the web page inside the application (as a WebView), then add forceWebView: true we’d do something like this:
launchURL(String url) async { if (await canLaunch(url)) { await launch(url, forceWebView: true); } else { throw 'Could not launch $url'; } }
And call it this way
onTap: () { const url = 'https://google.com'; launchURL(url); }
We will get output like below:
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.
Conclusion:
In this article, We have been through How to Open Web URL From Flutter Code?
Keep Learning !!!
Keep Fluttering.
FlutterAgency.com is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget Guide, Flutter Projects, Code libs and etc.
ready to get started?
Fill out the form below and we will be in touch soon!
"*" indicates required fields