Flutter automatically update (In app update) application when new version is available in both Ios And Apple

 Introduction

    Flutter In-App Update is a feature that allows you to prompt users to update your Flutter application when a new version is available. It ensures that users are using the latest version of your app, providing them with the latest features, improvements, and bug fixes. There are two types of in-app updates in Flutter.

First Get Package Or App File version 

Install Package_info_plus  Package in Pubspec.yaml File

Import the package in the Dart file where you want to use it:


import 'package:package_info_plus/package_info_plus.dart';


To get app file version or package version about the application package, you can use the following code:


String appversion='';

void getPackageInfo() async { PackageInfo packageInfo = await PackageInfo.fromPlatform();

  appversion=packageInfo.version; }


Next Get Flutter App play store and App store current Live version :


Import the package in the Dart file where you want to use it:


import 'package:upgrader/upgrader.dart';



To get Store version about the application , you can use the following code:


This Method return Store Version Both App Store and Play Store


Future<String?> getStoreVersion(String myAppBundleId) async {
     String? storeVersion;
 if (Platform.isAndroid) {
    PlayStoreSearchAPI playStoreSearchAPI = PlayStoreSearchAPI();
    Document? result =
    await playStoreSearchAPI.lookupById(myAppBundleId, country:'IN');
 if (result != null) storeVersion =                                       playStoreSearchAPI.version(result);
    log('PlayStore version: $storeVersion}');
 } else if (Platform.isIOS) {
    ITunesSearchAPI iTunesSearchAPI = ITunesSearchAPI();
    Map<dynamic, dynamic>? result =
    await iTunesSearchAPI.lookupByBundleId(myAppBundleId,country:'IN');
       if (result != null) storeVersion =                                 iTunesSearchAPI.version(result);
       log('AppStore version: $ storeVersion}');
 } else {
       storeVersion = null;
 }
     return storeVersion;
}

Next Step Define App To External Application (Like Appstore and Playstore) for App Updation :


Import the package in the Dart file where you want to use it:


import 'package: url_launcher/url_launcher.dart';


if (Platform.isAndroid || Platform.isIOS) {
final appId = Platform.isAndroid
? 'com.example.com' // insert android package Id
: 'com.example.com';    // insert App store App Id
final url = Uri.parse(
Platform.isAndroid
  ? "https://play.google.com/store/apps/details?id=$appId"
  : "https://apps.apple.com/app/id$appId",
);
launchUrl(url,mode: LaunchMode.externalApplication,).then((value) {
});
}

For above method using Update Dialog Box -> Update Button in Dart File Like Below:


























































Full Source Code :

import 'dart:developer';

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:upgrader/upgrader.dart';
import 'package:url_launcher/url_launcher.dart';


class InAppUpdate extends StatefulWidget {

const InAppUpdate({Key? key});

@override

State<InAppUpdate> createState() => _InAppUpdateState();

}

class _InAppUpdateState extends State<InAppUpdate>

@override

void initState() {

super.initState();

}

String appversion = '';

checkUpdate() async {

getPackageInfo(); //get App file version both ios and Android

String? storeversion2 = await getStoreVersion('com.example.com');

if (appversion != storeversion2) {

await showDialog<dynamic>(

context: context,

builder: (_) {

return Upgraderdialogbox(

storeversion: storeversion2,

);

}).then((value) {

exit(0);

});

}

}

void getPackageInfo() async {

PackageInfo packageInfo = await PackageInfo.fromPlatform();

appversion = packageInfo.version;

}

Future<String?> getStoreVersion(String myAppBundleId) async {

String? storeVersion;

if (Platform.isAndroid) {

PlayStoreSearchAPI playStoreSearchAPI = PlayStoreSearchAPI();

var result =

await playStoreSearchAPI.lookupById(myAppBundleId, country: 'IN');

if (result != null) storeVersion = playStoreSearchAPI.version(result);

log('PlayStore version: $storeVersion}');

} else if (Platform.isIOS) {

ITunesSearchAPI iTunesSearchAPI = ITunesSearchAPI();

Map<dynamic, dynamic>? result =

await iTunesSearchAPI.lookupByBundleId(myAppBundleId, country: 'IN');

if (result != null) storeVersion = iTunesSearchAPI.version(result);

log('AppStore version: $storeVersion}');

} else {

storeVersion = null;

}

return storeVersion;

}

@override

Widget build(BuildContext context) {

return Scaffold(

appBar: AppBar(title: const Text('In App Update')),

body: Container(

height: double.infinity,

width: double.infinity,

child: const Text('Define splash Screen Or Dashboard Screen')));

}

}

class Upgraderdialogbox extends StatefulWidget {

Upgraderdialogbox({

Key? key,

required this.storeversion,

}) : super(key: key);

String? storeversion;

@override

State<Upgraderdialogbox> createState() => ShowSearchDialogState();

}

class ShowSearchDialogState extends State<Upgraderdialogbox> {

@override

void initState() {

// TODO: implement initState

super.initState();

}

@override

Widget build(BuildContext context) {

final theme = Theme.of(context);

return AlertDialog(

content: SizedBox(

width: double.infinity,

height: 200,

child: Column(

mainAxisAlignment: MainAxisAlignment.spaceBetween,

crossAxisAlignment: CrossAxisAlignment.start,

children: [

Text(

"Update available",

style: theme.textTheme.titleMedium!

.copyWith(fontWeight: FontWeight.bold),

),

SizedBox(

height: 20,

),

Text(

"To use this app, download the latest version, You can keep using this app while downloading the update",

style: theme.textTheme.bodyText1,

),

IntrinsicHeight(

child: Row(

children: [

Container(

height: 12,

width: 30,

padding: EdgeInsets.all(10),

decoration: BoxDecoration(

borderRadius: BorderRadius.circular(5),

color: Colors.grey[ 200]),

child: Image.asset(

'Assets/SellerSymbol.png',

fit: BoxFit.fill,

),

),

Container(

alignment: Alignment.center,

padding: EdgeInsets.all(10),

child: Text(

'Version : ${widget.storeversion}',

style: theme.textTheme.bodySmall!

.copyWith(color: Colors.black),

),

),

],

),

),

Row(

mainAxisAlignment: MainAxisAlignment.end,

children: [

Container(

width: 40,

child: ElevatedButton(

style: ElevatedButton.styleFrom(

backgroundColor: theme.primaryColor,

),

onPressed: () async {

if (Platform.isAndroid || Platform.isIOS) {

final appId = Platform.isAndroid

? 'com.example.com'

: 'com.example.com';

final url = Uri.parse(

Platform.isAndroid

? "https://play.google.com/store/apps/details?id=$appId"

: "https://apps.apple.com/app/id$appId",

);

launchUrl(

url,

mode: LaunchMode.externalApplication,

).then((value) {

// exit(0);

});

}

},

child: Text('Update')),

),

],

)

],

),

),

);

}

}

Thank You

Previous Post Next Post