App version updates are essential when we develop apps. How do we implement this requirement in Flutter? Here is a simple method to upgrade the Flutter App version

First we need a few plugins: flutter_downloader link: pub.flutter-io.cn/packages/fl… Dio link: pub.flutter-io.cn/packages/di… Path_provider link: pub.flutter-io.cn/packages/pa… Open_file link: pub.flutter-io.cn/packages/op…

Add the above four plug-ins to pubspec.yaml

First we need to get the location of the APK, because when we download the latest version of APK, we need a path to store the APK. Reference the path_Provider plug-in

import 'package:path_provider/path_provider.dart';
Copy the code

Then create the _apkLocalPath function

// Get apK from external directory
  Future<String> get _apkLocalPath async {
    final directory = await getExternalStorageDirectory();
    return directory.path;
  }
Copy the code

Second, we need to create the apK installation function, referring to the Open_file plug-in,

import 'package:open_file/open_file.dart';
Copy the code
/ / / install the app
  Future<Null> _installApk() async {
    String path = await _apkLocalPath;
    await OpenFile.open(path + '/' + apkName);
  }
Copy the code

Step 3: Create the download function 0v0 that references flutter_downloader and dio plug-ins.

import 'package:dio/dio.dart';
import 'package:flutter_downloader/flutter_downloader.dart';
Copy the code
 / / / download
  Future<void> executeDownload() async {
    // ApK storage path
    final path = await _apkLocalPath;
    // Get the version
    var result = await Dio()
        .get("https://jderp.3mzz.cn/update/app/version.txt")
        .catchError((e) {
      print('Failed to get version number ==' ' + e);
    });
    apkName = 'test' + result.toString() + '.apk';
    File file = File(path + '/' + apkName);
    if (await file.exists()) await file.delete();

    / / download
    final taskId = await FlutterDownloader.enqueue(
        url: 'xxxx'.// Download the latest apK network address
        savedDir: path,
        fileName: apkName,
        showNotification: true,
        openFileFromNotification: true);
    FlutterDownloader.registerCallback((id, status, progress) {
      // When the download is complete, install is called
      if(taskId == id && status == DownloadTaskStatus.complete) { _installApk(); }}); }Copy the code

Just call executeDownload where you need it and here’s the code

import 'dart:io';
import 'package:dio/dio.dart';
import 'package:flutter_downloader/flutter_downloader.dart';
import 'package:path_provider/path_provider.dart';
import 'package:open_file/open_file.dart';

/// Version upgrade
class VersionUpgrade {
  String apkName;

// Get apK from external directory
  Future<String> get _apkLocalPath async {
    final directory = await getExternalStorageDirectory();
    return directory.path;
  }

  / / / download
  Future<void> executeDownload() async {
    final path = await _apkLocalPath;
    var result = await Dio()
        .get("https://jderp.3mzz.cn/update/app/version.txt")
        .catchError((e) {
      print('Failed to get version number ==' ' + e);
    });
    apkName = 'xxx' + result.toString() + '.apk';
    File file = File(path + '/' + apkName);
    if (await file.exists()) await file.delete();

    / / download
    final taskId = await FlutterDownloader.enqueue(
        url: 'https://jderp.3mzz.cn/update/app/jderp.apk',
        savedDir: path,
        fileName: apkName,
        showNotification: true,
        openFileFromNotification: true);
    FlutterDownloader.registerCallback((id, status, progress) {
      // When the download is complete, install is called
      if(taskId == id && status == DownloadTaskStatus.complete) { _installApk(); }}); }/ / / install the app
  Future<Null> _installApk() async {
    String path = await _apkLocalPath;
    await OpenFile.open(path + '/'+ apkName); }}Copy the code