Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - Android Studio - won't recognize the url_launcher import properly

I'm trying to make my app open weblinks and/or installed apps that the user have on his/her phone.

Running flutter --version will get:

Flutter 1.17.2 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 5f21edf8b6 (6 days ago) • 2020-05-28 12:44:12 -0700
Engine • revision b851c71829
Tools • Dart 2.8.3

Im using:

    environment:
  sdk: ">=2.7.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter


  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^0.1.3
  url_launcher: ^5.4.2

dev_dependencies:
  flutter_test:
    sdk: flutter

I import this in my code:

import 'package:url_launcher/url_launcher.dart';

I get the following error:

Compiler message:
Error: Could not resolve the package 'url_launcher' in 'package:url_launcher/url_launcher.dart'.
lib/main.dart:4:8: Error: Not found: 'package:url_launcher/url_launcher.dart'
import 'package:url_launcher/url_launcher.dart';
       ^

if I use the function I got from Google tutorial:

  Future<void> _launchUniversalLinkIos(String url) async {
    if (await canLaunch(url)) {
      final bool nativeAppLaunchSucceeded = await launch(
        url,
        forceSafariVC: false,
        universalLinksOnly: true,
      );
      if (!nativeAppLaunchSucceeded) {
        await launch(url);
      }
    }
  }

I get the above error plus a few of this errors under it:

lib/main.dart:40:15: Error: The method 'canLaunch' isn't defined for the class '_MyStatefulWidgetState'.
 - '_MyStatefulWidgetState' is from 'package:fluttertodolist/main.dart' ('lib/main.dart').
Try correcting the name to the name of an existing method, or defining a method named 'canLaunch'.
    if (await canLaunch(url)) {
              ^^^^^^^^^
lib/main.dart:41:51: Error: The method 'launch' isn't defined for the class '_MyStatefulWidgetState'.
 - '_MyStatefulWidgetState' is from 'package:fluttertodolist/main.dart' ('lib/main.dart').
Try correcting the name to the name of an existing method, or defining a method named 'launch'.
      final bool nativeAppLaunchSucceeded = await launch(
                                                  ^^^^^^
lib/main.dart:47:15: Error: The method 'launch' isn't defined for the class '_MyStatefulWidgetState'.
 - '_MyStatefulWidgetState' is from 'package:fluttertodolist/main.dart' ('lib/main.dart').
Try correcting the name to the name of an existing method, or defining a method named 'launch'.
        await launch(url);
              ^^^^^^

I've search for hours and all I found in sStackoverflow questions are not helping, and in also in the tutorials and everywhere else. everyone suggesting to use the way I already use.

like image 547
Lidor Eliyahu Shelef Avatar asked Sep 05 '25 02:09

Lidor Eliyahu Shelef


1 Answers

The solution for it was one of the 2 things I did:

1) there was an update for android studio + flutter plugin

2) I removed the plugin from the pubspec.yaml and from the pubspec.lock then I compiled again and then I added them again to the pubspec.yaml and compiles

after this 2 it worked, not sure which one b/c they were together.

like image 112
Lidor Eliyahu Shelef Avatar answered Sep 07 '25 23:09

Lidor Eliyahu Shelef