Home > Software design >  Can not open my Url in flutter with Url_launcher
Can not open my Url in flutter with Url_launcher

Time:11-07

i am trying to open a specific instagram url in my app , i've tried all possible ways that i could find but no matter what i do i get a message that says component name for (url) is null then a message that says component name for https://flutter.dev is null , i've added queries , used canluanch method , canlaunchurL , canlaunchurl methods , but i still get the same thing here is the final attempt that i made :

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

class MilaInformation extends StatelessWidget {
  const MilaInformation({super.key});

  @override
  Widget build(BuildContext context) {
    var h = MediaQuery.of(context).size.height;
    var w = MediaQuery.of(context).size.width;
    return SafeArea(
      child: Scaffold(
        backgroundColor: Colors.white,
        body: SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              ClipRRect(
                borderRadius: BorderRadius.only(
                    bottomLeft: Radius.circular(w * 0.09),
                    bottomRight: Radius.circular(w * 0.09)),
                child: Container(width: w, height: h / 1.6, color: Colors.pink),
              ),
              SizedBox(
                height: h * 0.02,
              ),
              Padding(
                padding: const EdgeInsets.all(15.0),
                child: Text(
                  " ؟ Mila Rose من هي",
                  style: TextStyle(
                    fontSize: 30,
                    color: Colors.black,
                  ),
                  textDirection: TextDirection.ltr,
                ),
              ),
              Padding(
                padding: const EdgeInsets.fromLTRB(15, 0, 15, 5),
                child: Text(
                  " هي أول شركة عربية مختصة بتجارة الورود مقرها الرئيسي في هولندا , تقدم ميلا روز أفضل الخدمات في مجال الزهور على نطاق العالم الواسع",
                  style: TextStyle(fontSize: 20, color: Colors.grey[500]),
                  textDirection: TextDirection.rtl,
                ),
              ),
              SizedBox(
                height: 15,
              ),
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    GestureDetector(
                        child: Padding(
                          padding: EdgeInsets.all(15.0),
                          child: Container(
                            height: 25,
                            width: 25,
                            child: Image.asset(
                              "assets/images/insta.jfif",
                            ),
                          ),
                        ),
                        onTap: () async {
                          String url =
                              "https://www.instagram.com//milarosenederland/?igshid=YmMyMTA2M2Y=";
                          final uri = Uri.parse(url);
                          if (await canLaunchUrl(uri)) {
                            await launchUrl(uri,
                                mode: LaunchMode.externalApplication);
                          }
                        }),
                    Text(" 974-6001-1002"),
                    SizedBox(
                      width: 10,
                    ),
                    Text(
                      "للتواصل :",
                      textDirection: TextDirection.rtl,
                    ),
                  ],
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}

and here is my manifest.xml :

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mila_rose">
   <application
        android:label="mila_rose"
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application> 
    <queries> 
    <intent>
  <action android:name="android.intent.action.VIEW" />
  <category android:name="android.intent.category.BROWSABLE" />
  <data android:scheme="https" />
</intent> 
<intent>
  <action android:name="android.media.browse.MediaBrowserService" />
</intent>
    </queries>
</manifest> 

any possible solutions ?

CodePudding user response:

First stop project then run these:

flutter clean
flutter pub get

then start project again

CodePudding user response:

Basically what is happening here is that flutter is successfully receiving the String that you are passing, but it does not know what to do with it. In order to tell it how to process that specific string, we need to mention its 'scheme'.

For eg. Lets try to access https://flutter.dev correctly through your code

Instead of making a String url, make a uri mentioning its scheme right away

final Uri toLaunch =
        Uri(scheme: 'https', host: 'www.flutter.dev');

At the same time, when using this to access a sub path, we also mention it explicitly

final Uri toLaunch =
            Uri(scheme: 'https', host: 'www.instagram.com', path: 'milarosenederland/?igshid=YmMyMTA2M2Y=/');

CodePudding user response:

use launchUrlString() instead

 launchUrlString("https://www.instagram.com//milarosenederland/?igshid=YmMyMTA2M2Y=",
mode:LaunchMode.externalApplication);
  • Related