'How to launch URL in Flutter?

NOTE: I've already searched for ages about this problem & read every similar problem on StackOverflow

I'm making Flutter app and I want to launch a URL on button click (Canvas based).

Everything was okay, until I used "url_launcher".

I'm using Flutter 1.13.6(Latest).

Below is the redirection function I'm using.

import 'package:url_launcher/url_launcher.dart' as URLLaucher;

abstract class URL {
  static launchURL(String url) async {
    url = Uri.encodeFull(url);
    if (await URLLaucher.canLaunch(url)) {
      await URLLaucher.launch(url);
    } else {
      throw 'Unknown error, can\'t launch the URL. Холбоост нэвтрэхэд алдаа гарлаа';
    }
  }
}

And this is the error log I'm gettin' when I run "flutter run" command.


FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':url_launcher'.
> Could not resolve all artifacts for configuration ':url_launcher:classpath'.
   > Could not download gradle.jar (com.android.tools.build:gradle:3.4.2)
      > Could not get resource 'https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/3.4.2/gradle-3.4.2.jar'.
         > Read timed out
   > Could not download builder.jar (com.android.tools.build:builder:3.4.2)
      > Could not get resource 'https://dl.google.com/dl/android/maven2/com/android/tools/build/builder/3.4.2/builder-3.4.2.jar'.
         > Premature end of Content-Length delimited message body (expected: 31038805; received: 2359280
> Failed to notify project evaluation listener.
   > Could not get unknown property 'android' for project ':url_launcher' of type org.gradle.api.Project.
   > Could not find method implementation() for arguments [project ':url_launcher_web'] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
   > Could not find method implementation() for arguments [project ':url_launcher_macos'] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org


Solution 1:[1]

I had the same error. So I upgraded flutter to v1.12.13 and used url_launcher5.3.0 and it worked.

Solution 2:[2]

this happens when you do a hot restart after you added something to your pubspec.yaml - just close the app and install it again

Solution 3:[3]

pubspec.yaml

url_launcher: ^6.0.6

main.dart

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

const _url = 'https://flutter.dev';

void main() => runApp(
  const MaterialApp(
    home: Material(
      child: Center(
        // ignore: deprecated_member_use
        child: RaisedButton(
          onPressed: _launchURL,
          child: Text('Bappa\'s apps in playstore'),
        ),
      ),
    ),
  ),
);

void _launchURL() async {

  launch("https://play.google.com/store/search?q=pub%3ABAPPA%20SAIKH&c=apps" );
}

Solution 4:[4]

I used this syntax and worked for me

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 VerySeriousSoftwareEndeavours
Solution 2 Edin
Solution 3 BAPPA SAIKH
Solution 4 Dhruvin Moradiya