1

I want to implement conditions in my existing app that a particular app is installed or not.

Conditions:

the first app is that which is I'm going to build

Second app that is required for run first app

  1. If the app is installed (Second app) then run HomePage() in the first app
  2. If the app is not installed then show a pop-up or alert for install the app.

Root Code of the first app

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
    future: Future.delayed(Duration(seconds: 2)),
        builder: (context, AsyncSnapshot snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return MaterialApp(
              home: Splash(),
              debugShowCheckedModeBanner: false,
            );
          } else {
            return StreamBuilder(
              stream: Connectivity().onConnectivityChanged,
              builder: (context, AsyncSnapshot<ConnectivityResult> snapshot) {
                return snapshot.data == ConnectivityResult.mobile ||
                        snapshot.data == ConnectivityResult.wifi
                    ? MaterialApp(
                        title: 'mFollower',
                        theme: ThemeData(
                          primarySwatch: Colors.blue,
                        ),
                        home: Homepage(),
                        debugShowCheckedModeBanner: false,
                      )
                    : NoInternet();
              },
            );
          }

        });
  }
}
M.K. Mandawar
  • 69
  • 1
  • 12

1 Answers1

1

This can be achieved using the package device_apps package. This will return the list of apps installed in the Android device.

// All the apps installed in the device
List<Application> apps = await DeviceApps.getInstalledApplications();

So for your requirement you can do something like below

Future<bool> isAppInstalled() {
    return DeviceApps.getInstalledApplications().then((value) =>
        value.any((element) => element.packageName == 'com.example.app')); // Your app package id to check.
}

And inside build method use Future builder to get the result.

FutureBuilder<bool>(
        future: apps(),
        builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
          if (snapshot.hasData && snapshot.data) {
            // App is installed
            return Container(
              color: Colors.green,
            );
          } else {
            // App is not installed
            return Container(color: Colors.redAccent);
          }
        })

Unfortunately this plugin is supported only for ANDROID

Swaminathan V
  • 4,663
  • 2
  • 22
  • 32
  • thanks for your answer, but maybe you don't understand what I'm going to say exactly. – M.K. Mandawar Jul 10 '21 at 04:30
  • Let's understand with a real-life example: There are two apps on my phone i.e. `First App` and `Second App`. `Second App` already developed and published. `First app` under development. If the `Second App` is installed in my mobile then open the `First App` `HomePage()` state otherwise redirect to another state like `noInstalled State()`. I have shared the root code of `First App` in it a condition already applied for check internet connection – M.K. Mandawar Jul 10 '21 at 04:38
  • OK. So you are trying to open the `First app` if it is installed in the device when user attempts to open the `Second app` which is already installed. User -> Opens Second App -> If First app is installed open it or else say some error ? – Swaminathan V Jul 10 '21 at 06:23
  • Yes exactly. How can I implement in existing code which are above – M.K. Mandawar Jul 11 '21 at 00:37
  • You need to implement the above logic in the second app. – Swaminathan V Jul 11 '21 at 13:03
  • I'm new to flutter so unable to implement. Can you help me with this – M.K. Mandawar Jul 12 '21 at 07:29
  • You can try the above app with default template app that will be created when creating a new flutter project. Use that to perform your logic and then implement them in the actual app. – Swaminathan V Jul 12 '21 at 08:31