41

How can I open a specific URL of the PlayStore/AppStore with flutter on Android and IOS, depending on which smartphone it is executed? I mean I want to open the application and not a browser or something like this.

In this thread I found some native way for android but how can I do this with flutter?

final String appPackageName = getPackageName(); // getPackageName() from Context or Activity object
try {
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
} catch (android.content.ActivityNotFoundException anfe) {
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
}

If there is currently no way to do this, it would be a nice feature to implement for the plugin url_launcher.

GreenTigerEye
  • 4,761
  • 8
  • 21
  • 33
  • Note that you can get your app's package name in pure Dart code, without writing any Java, by using the [flutter_android](https://pub.dartlang.org/packages/flutter_android) plugin's [`Context.packageName`](https://pub.dartlang.org/documentation/flutter_android/latest/android_content/Context/packageName.html) getter. – Arto Bendiken Nov 03 '18 at 07:36

6 Answers6

47

You can use this Library,

Basically, To use this plugin, add launch_review as a dependency in your pubspec.yaml file.

launch_review: ^1.0.1

To use :

 import 'package:launch_review/launch_review.dart'; 

Then invoke the static launch method of LaunchReview anywhere in your Dart code. If no arguments are provided, it will consider the current package.

LaunchReview.launch();

To open the App Store page for any other applications, you can pass the app Id.

LaunchReview.launch(androidAppId: <package name>,
                iOSAppId: <ios app id>);
Naroju
  • 2,567
  • 4
  • 22
  • 40
8

You can use the url_launcher package for opening the appstore/playstore as follows :

      _launchURL(String url) async {
         if (await canLaunch(url)) {
             await launch(url);
         } 
         else {
             throw 'Could not launch $url';
         }
       }
developer43
  • 83
  • 1
  • 6
  • using this as a uri works "https://apps.apple.com/us/app/appname/id1485117463" but first safari shows up for a second or two then the safari itself redirects it to the actual app :D – Mehmet Filiz Jun 28 '20 at 19:48
7

You can do something similar in flutter:

import 'package:url_launcher/url_launcher.dart';

try {
  launch("market://details?id=" + appPackageName);
} on PlatformException catch(e) {
    launch("https://play.google.com/store/apps/details?id=" + appPackageName);        
} finally {
  launch("https://play.google.com/store/apps/details?id=" + appPackageName);        
}

The exception/catch does not seem to work for some reason so adding 'finally' did the trick, finally :)

K Vij
  • 1,345
  • 1
  • 11
  • 16
  • 2
    You cannot catch an exception that occurs in a function/method returning `Future` unless you put `await` when you call it. – kaboc May 27 '20 at 05:29
  • ahhh I see, but how come the 'finally' part gets executed? – K Vij May 28 '20 at 01:24
  • 2
    The `finally` block is executed regardless of whether an exception occurred of not. I suspect that your code tries to open the store twice. – kaboc May 28 '20 at 10:19
  • hmm... i don't see that behavior... I'll test again. – K Vij May 28 '20 at 18:21
  • On flutter web, on Chrome Mobile the first method does not work, hence it opens the play store on the ugly web site. – MappaM Mar 22 '21 at 11:57
  • What about app store ? that is only for anrdoid, how to open app store – Kristi Jorgji Jul 28 '21 at 15:20
  • You don't need `try/catch`, You can use `if/else` with the `canLaunch()` method of `url_launcher` package. – Tayan Sep 05 '21 at 01:50
  • After using it for a while, I've realized that it opens the stock store (App Gallery) instead of Google Play on Huawei phones. To learn more, look here: https://developer.android.com/distribute/marketing-tools/linking-to-google-play#android-app – Tayan Sep 17 '21 at 16:32
4

You can use this plug in. here

Super simple. To use this pulg in, just write the package name (app id) on your dart code like this.

OpenAppstore.launch(androidAppId: "com.facebook.katana&hl=ko", iOSAppId: "284882215")
moonyoung
  • 41
  • 1
4

This is like the most popular answer to this question, but without the need of saying the Android package. In addition, this solution doesn't show a toast on Android saying "Please Rate Application".

It depends on open_store and package_info_plus.

const appStoreId = "1234567890"; // Your app's App Store ID
final packageName = (await PackageInfo.fromPlatform()).packageName;

OpenStore.instance.open(
  androidAppBundleId: packageName,
  appStoreId: appStoreId,
);
Roc Boronat
  • 10,537
  • 4
  • 43
  • 53
1

You can also try store_launcher

Example

StoreLauncher.openWithStore(appId);
heqingbao
  • 258
  • 2
  • 5
  • It doesn't support Sound Null Safety :·( In addition, Android package name could be automatically read from the app build itself... – Roc Boronat Mar 17 '22 at 11:54