I want the users of my app to always have the latest version. If they don't have the latest version, it should download the latest version from play store automatically on app startup. I'm using in_app_update
for that. I'm performing Performs immediate update
Below is the code of splash screen which came after main
. Here I check for update in route
function, if update is available then perform update and navigate to homeView
, If not simply navigate to homeView
But app never informed new user about update whenever new version is uploaded on playstore. They have to manually go to playstore to update an app. Why is that? Am I doing something wrong in a code or do I need to do something extra?
import 'package:in_app_update/in_app_update.dart';
class SplashView extends StatefulWidget {
@override
_SplashViewState createState() => _SplashViewState();
}
class _SplashViewState extends State<SplashView> {
AppUpdateInfo _updateInfo; // --- To check for update
@override
void initState() {
super.initState();
startTimer();
}
startTimer() async {
var duration = Duration(milliseconds: 1500);
return Timer(duration, route);
}
route() async {
await checkForUpdate();
bool visiting = await ConstantsFtns().getVisitingFlag();
if (_updateInfo?.updateAvailable == true) {
InAppUpdate.performImmediateUpdate().catchError((e) => _showError(e));
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => HomeView(),
),
);
} else {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => HomeView(),
),
);
}
}
Future<void> checkForUpdate() async {
InAppUpdate.checkForUpdate().then((info) {
setState(() {
_updateInfo = info;
});
}).catchError((e) => _showError(e));
}
void _showError(dynamic exception) {
_scaffoldKey.currentState.showSnackBar(SnackBar(
content:
Text("Exception while checking for error: " + exception.toString()),
));
@override
Widget build(BuildContext context) {
return Material(
child: AnimatedSplashScreen(
.............................
),
);
}
}
I don't know why suggested solution of similar question is not working for me. https://stackoverflow.com/a/62129373/7290043
By updating app itself without asking user is policy violation that may lead you to suspension of app. read this before trying anything like this: Device and Network Abuse
You can ask users to update app whenever new update is available.
Edit:
code for Finding latest version on playstore:
getAndroidStoreVersion(String id) async {
final url = 'https://play.google.com/store/apps/details?id=$id';
final response = await http.get(url);
if (response.statusCode != 200) {
print('Can\'t find an app in the Play Store with the id: $id');
return null;
}
final document = parse(response.body);
final elements = document.getElementsByClassName('hAyfc');
final versionElement = elements.firstWhere(
(elm) => elm.querySelector('.BgcNfc').text == 'Current Version',
);
dynamic storeVersion = versionElement.querySelector('.htlgb').text;
return storeVersion;
}
For mobile version: i have used this package Package_info
To check if latest version is available or not:
getUpdateInfo(newVersion) async {
PackageInfo packageInfo = await PackageInfo.fromPlatform();
String playStoreVersion =
await getAndroidStoreVersion(packageInfo.packageName);
int playVersion = int.parse(playStoreVersion.trim().replaceAll(".", ""));
int CurrentVersion=
int.parse(packageInfo.version.trim().replaceAll(".", ""));
if (playVersion > CurrentVersion) {
_showVersionDialog(context, packageInfo.packageName);
}
}
You can design your pop up as per your convenience.
Yes @Mayur exactly. I know. I;m trying to show the prompt first as mentioned in
immediate update flow
this docs screenshot pub.dev/packages/in_app_update But I'm unable to show that promptI've added the code. I hope it will help
@FaizanKamal i have updated my answer with code. as playstore doesn't provide any API here's a workaround.
Thanks for such a vital info. I only have one confusion now. If
(playVersion > CurrentVersion)
is true then how I'm gonna update app on "update" button click of dialog inside if condition. Should I use URL launcher and send them to play store or is there a better way to do this?It's recommended to use url launcher and send user to playstore let user update by their self.