0
0
FlutterHow-ToBeginner · 3 min read

How to Use url_launcher in Flutter to Open URLs

To use url_launcher in Flutter, add it to your pubspec.yaml, import it, and call launchUrl() with a Uri. This lets your app open web pages, phone dialers, or email apps easily.
📐

Syntax

The main function to open URLs is launchUrl(). You pass a Uri object representing the link you want to open. Optionally, you can specify launch options like opening in an external browser.

  • Uri: The link to open, e.g., Uri.parse('https://flutter.dev').
  • launchUrl(uri): Opens the given URI.
dart
import 'package:url_launcher/url_launcher.dart';

Future<void> openLink() async {
  final Uri url = Uri.parse('https://flutter.dev');
  if (!await launchUrl(url)) {
    throw 'Could not launch $url';
  }
}
💻

Example

This example shows a simple Flutter app with a button that opens the Flutter website when tapped.

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

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

class MyApp extends StatelessWidget {
  final Uri _url = Uri.parse('https://flutter.dev');

  Future<void> _launchUrl() async {
    if (!await launchUrl(_url)) {
      throw 'Could not launch $_url';
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('url_launcher Example')),
        body: Center(
          child: ElevatedButton(
            onPressed: _launchUrl,
            child: const Text('Open Flutter Website'),
          ),
        ),
      ),
    );
  }
}
Output
A screen with an app bar titled 'url_launcher Example' and a centered button labeled 'Open Flutter Website'. Tapping the button opens the Flutter homepage in the device's default browser.
⚠️

Common Pitfalls

  • Not adding url_launcher to pubspec.yaml dependencies.
  • Forgetting to import package:url_launcher/url_launcher.dart.
  • Using launch() which is deprecated; use launchUrl() instead.
  • Not checking if the URL can be launched before launching.
  • Passing a string instead of a Uri object.
dart
import 'package:url_launcher/url_launcher.dart';

// Wrong: passing string directly
// await launch('https://flutter.dev');

// Right: use Uri and launchUrl
final Uri url = Uri.parse('https://flutter.dev');
await launchUrl(url);
📊

Quick Reference

Here is a quick summary of key points when using url_launcher:

ActionCode Example
Add dependencyurl_launcher: ^6.1.7
Import packageimport 'package:url_launcher/url_launcher.dart';
Create Urifinal Uri url = Uri.parse('https://example.com');
Launch URLawait launchUrl(url);
Check launch successif (!await launchUrl(url)) { throw 'Could not launch'; }

Key Takeaways

Add url_launcher to pubspec.yaml and import it before use.
Use launchUrl() with a Uri object to open links safely.
Always check if the URL can be launched to avoid errors.
Avoid deprecated launch() method; prefer launchUrl().
You can open websites, phone calls, emails, and more with url_launcher.