Skip to content

How to force update and not to let user to use app? #17

@NTMS2017

Description

@NTMS2017

Hi,

I need to force user to update app always. If they not going to update I should block user not to use app. Also I created a example app and try to use your example, but it didn't work and I guess there was no app that I am running in Google Play.

Later I update my app package name which is already in app store and I keep getting error as shown below.

I/flutter ( 7751): PlatformException(Failed to bind to the service., null, null)

Last, your example uses a button to check for update how to use this without button but in initState? Thanks

Note: I all need is to check for Update initial app initialization than force to make performImmediateUpdate().

My original app:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await SystemChrome.setEnabledSystemUIOverlays([]);
  await SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    SystemChrome.setEnabledSystemUIOverlays([]);
    SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);

    return new MaterialApp(
      // TODO: SUPPORTED LOCALE
      localizationsDelegates: [
        const LangDelegate(),
        GlobalMaterialLocalizations.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalMaterialLocalizations.delegate,
        const FallbackCupertinoLocalisationsDelegate(),
      ],
      supportedLocales: [
        const Locale('tr', 'TR'),
        const Locale('en', 'US'),
        const Locale('ru', 'RU'),
      ],

      debugShowCheckedModeBanner: false,
      title: 'My Cep',
      theme: new ThemeData(
        primaryColor: capitalDarkGreen,
        fontFamily: "Roboto",
      ),
      home: new MyMainHomePage(),
    );
  }
}

And in MyMainHomePage I have PackageInfo and Connectivity in initState. So where I code the android app update?

Here is the full code (I am not sure where I am making mistake):

import 'package:flutter/material.dart';
import 'package:upgrader/upgrader.dart';
import 'dart:io' show Platform;
import 'dart:async';
import 'package:in_app_update/in_app_update.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  AppUpdateInfo _updateInfo;
  GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey();
  bool _flexibleUpdateAvailable = false;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    checkForUpdate();
    //InAppUpdate.performImmediateUpdate().catchError((e) => _showError(e));
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  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.toString())));
    print(exception.toString());
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        key: _scaffoldKey,
        appBar: AppBar(
          title: const Text('In App Update Example App'),
        ),
        body: _updateInfo?.updateAvailable == false
            ? new CircularProgressIndicator()
            : Platform.isAndroid == true
                ? Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Column(
                      children: <Widget>[
                        Center(
                          child: Text('Update info: $_updateInfo'),
                        ),
                        RaisedButton(
                          child: Text('Check for Update'),
                          onPressed: () => checkForUpdate(),
                        ),
                        RaisedButton(
                          child: Text('Perform immediate update'),
                          onPressed: _updateInfo?.updateAvailable == true
                              ? () {
                                  InAppUpdate.performImmediateUpdate().catchError((e) => _showError(e));
                                }
                              : null,
                        ),
                        RaisedButton(
                          child: Text('Start flexible update'),
                          onPressed: _updateInfo?.updateAvailable == true
                              ? () {
                                  InAppUpdate.startFlexibleUpdate().then((_) {
                                    setState(() {
                                      _flexibleUpdateAvailable = true;
                                    });
                                  }).catchError((e) => _showError(e));
                                }
                              : null,
                        ),
                        RaisedButton(
                          child: Text('Complete flexible update'),
                          onPressed: !_flexibleUpdateAvailable
                              ? null
                              : () {
                                  InAppUpdate.completeFlexibleUpdate().then((_) {
                                    _scaffoldKey.currentState.showSnackBar(SnackBar(content: Text('Success!')));
                                  }).catchError((e) => _showError(e));
                                },
                        )
                      ],
                    ),
                  )
                : UpgradeAlert(
                    child: Center(child: Text('Checking...')),
                  ));
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions