Thứ Ba, 9 tháng 7, 2019

Thông báo cập nhật trong flutter


1. THÊM 3 THƯ VIỆN + KẾT NỐI FIREBASE



package_info: ^0.4.0+4 : https://pub.dev/packages/package_info#-readme-tab-
url_launcher: ^5.0.3 : https://pub.dev/packages/url_launcher#-installing-tab-
firebase_remote_config: ^0.2.0+3 : https://pub.dev/packages/firebase_remote_config#-installing-tab-

2. CODE  : main.dart
import 'dart:io';import 'package:url_launcher/url_launcher.dart';import 'package:package_info/package_info.dart';import 'package:firebase_remote_config/firebase_remote_config.dart';
@override
void initState() {
  
try {
    versionCheck(context);
  } 
catch (e) {
    print(e);
  }
  
super.initState();
}


const APP_STORE_URL ='https://apps.apple.com/us/app/tu%E1%BB%95i-tr%E1%BA%BB-online/id974433568?l=vi&ls=1';const PLAY_STORE_URL ='https://play.google.com/store/apps/details?id=YOUR-APP-ID';

versionCheck(context) async {
  //Get Current installed version of app
  
final PackageInfo info = await PackageInfo.fromPlatform();
  double currentVersion = double.
parse(info.version.trim().replaceAll(".", ""));
  //Get Latest version info from firebase config
  
final RemoteConfig remoteConfig = await RemoteConfig.instance;
  
try {
    // Using default duration to force fetching from remote server.
    
await remoteConfig.fetch(expiration: const Duration(seconds: 0));
    
await remoteConfig.activateFetched();
    remoteConfig.getString('force_update_current_version');
    double newVersion = double.
parse(remoteConfig
        .getString('force_update_current_version')
        .trim()
        .replaceAll(".", ""));
    
if (newVersion > currentVersion) {
      _showVersionDialog(context);
    }
  } 
on FetchThrottledException catch (exception) {
    // Fetch throttled.
    print(exception);
  } 
catch (exception) {
    print('Unable to fetch remote config. Cached or default values will be '
        'used');
  }
}

_showVersionDialog(context) async {
  
await showDialog<String>(
    context: context,
    barrierDismissible: 
false,
    builder: (BuildContext context) {
      String title = "New Update Available";
      String message =
          "There is a newer version of app available please update it now.";
      String btnLabel = "Update Now";
      String btnLabelCancel = "Later";
      
return Platform.isIOS
          ? new CupertinoAlertDialog(
              title: Text(title),
              content: Text(message),
              actions: <Widget>[
                FlatButton(
                  child: Text(btnLabel),
                  onPressed: () => _launchURL(
APP_STORE_URL),
                ),
                FlatButton(
                  child: Text(btnLabelCancel),
                  onPressed: () => Navigator.
pop(context),
                ),
              ],
            )
          : 
new AlertDialog(
              title: Text(title),
              content: Text(message),
              actions: <Widget>[
                FlatButton(
                  child: Text(btnLabel),
                  onPressed: () => _launchURL(
PLAY_STORE_URL),
                ),
                FlatButton(
                  child: Text(btnLabelCancel),
                  onPressed: () => Navigator.
pop(context),
                ),
              ],
            );
    },
  );
}

_launchURL(String url) async {
  
if (await canLaunch(url)) {
    
await launch(url);
  } 
else {
    
throw 'Could not launch $url';
  }
}

3. LÊN TRÌNH ĐIỀU KHIỂN FIREBASE

- Vào remote config thêm "force_update_current_version" giá trị 1.1.0























Không có nhận xét nào:

Đăng nhận xét

How to reload app in flutter

  Phoenix . rebirth (context);