Flutter应用更新检测插件uptodate的使用

Flutter应用更新检测插件uptodate的使用

内容

UpToDate Action

此操作帮助你保持仓库最新,通过在依赖项发生变化时创建问题。

使用

  1. 创建一个工作流如下:
name: uptodate

on:
  schedule:
    - cron: "0 0 * * *"

jobs:
  uptodate:
    runs-on: ubuntu-latest
    steps:
      # 首先你需要检出你的仓库。
      - uses: actions/checkout@v2
      - uses: fischerscode/uptodate@v0.7
        with:
          # 配置文件的位置。
          # 默认为'.uptodate.yaml'
          config: ''

          # 用于创建问题的令牌。
          # 默认为${{ github.token }}
          token: ''

          # 新问题的目标仓库。
          # 默认为${{ github.repository }}
          repository: ''
  1. 创建配置文件.uptodate.yaml文档)。

UpToDate作为命令行工具

dart pub global activate uptodate

或者

使用一个预构建的可执行文件

使用

A tool that helps you to keep your repository up to date.

Usage: uptodate <command> [arguments]

全局选项:
-h, --help                打印此用法信息。
-f, --file (mandatory)    配置文件。
-v, --[no-]verbose        详细输出。

可用命令:
  check     检查配置文件中的依赖项是否有更新。
  github    检查更新并在你的GitHub仓库中创建问题。
  version   打印uptodate版本。

运行"uptodate help <command>"以获取更多关于命令的信息。

配置

Uptodate使用一个单独的文件进行配置,其中包含所有依赖项及其当前使用的版本。

配置文件

dependencies:   # 包含依赖项的未排序列表。
  - name: testdependency        # 依赖项的名称
    type: typ                   # 依赖项的类型
    currentVersion: 1.2.3       # 依赖项的当前版本
    issueLabels:                # 添加到问题的标签
      - dependencies
      - test
defaultIssueTitle: "Update $name to $latestVersion"     # 默认问题标题
defaultIssueBody: "Update $name to $latestVersion"      # 默认问题正文

defaultIssueTitledefaultIssueBody中允许以下变量:

  • $name: 依赖项的名称
  • $currentVersion: 依赖项的当前版本
  • $latestVersion: 可用的最新版本

使用issueTitle可以覆盖每个依赖项的defaultIssueTitle。 使用issueBody可以覆盖每个依赖项的defaultIssueBody

依赖类型

有多种依赖类型:

Web依赖

调用URL以接收依赖项的最新版本。

dependencies:
  - name: testdependency        # 依赖项的名称。(必需)
    type: web                   # 依赖项的类型。(必需)
    currentVersion: 1.2.3       # 依赖项的当前版本。(必需)
    url: 'http://example.com'   # 调用的URL。(必需)
JSON Web依赖

调用URL以接收包含最新版本的JSON文档。

dependencies:
  - name: testdependency          # 依赖项的名称。(必需)
    type: webjson                 # 依赖项的类型。(必需)
    currentVersion: 1.2.3         # 依赖项的当前版本。(必需)
    url: 'http://example.com'     # 调用的URL。(必需)
    path: args.versions.0.version # 到最新版本的路径。(默认为空)
    prefix: v                     # 语义版本的前缀。
                                  # (v1.2.3而不是1.2.3)
                                  # 默认为空
YAML Web依赖

调用URL以接收包含最新版本的单一YAML文档。

dependencies:
  - name: testdependency          # 依赖项的名称。(必需)
    type: webyaml                 # 依赖项的类型。(必需)
    currentVersion: 1.2.3         # 依赖项的当前版本。(必需)
    url: 'http://example.com'     # 调用的URL。(必需)
    path: args.versions.0.version # 到最新版本的路径。(默认为空)
    prefix: v                     # 语义版本的前缀。
                                  # (v1.2.3而不是1.2.3)
                                  # 默认为空
GitHub依赖

使用GitHub仓库来接收最新版本。 版本从最新的(非预发布)版本或标签中获取,具有正确的前缀。

dependencies:
  - name: gitdependency            # 依赖项的名称。(必需)
    type: github                   # 依赖项的类型。(必需)
    currentVersion: v1.2.3         # 依赖项的当前版本。(必需)
    repo: 'fischerscode/uptodate'  # 仓库。(必需)
    isTag: true                    # 是否版本由最新标签而不是发布决定(默认为false)
    path: tag_name                 # 到最新版本的路径。(默认为tag_name)
    prefix: v                      # 语义版本的前缀。
                                   # 设置后,不匹配前缀的发布/标签将被忽略。
                                   # (v1.2.3而不是1.2.3)
                                   # 默认为空
Helm依赖

使用Helm仓库来接收最新版本。 版本通过YAML路径获取。

dependencies:
  - name: helmdependency           # 依赖项的名称。(必需)
    type: helm                     # 依赖项的类型。(必需)
    currentVersion: v1.2.3         # 依赖项的当前版本。(必需)
    repo: 'https://helm.traefik.io/traefik'  # 仓库。(必需)
    chart: traefik                      # 图表。(必需)
    prefix: v                           # 语义版本的前缀。
                                        # (v1.2.3而不是1.2.3)
                                        # 默认为空
    path: 'entries.traefik.0.version'   # 到最新版本的路径。(默认为'entries.$chart.0.version')

更多关于Flutter应用更新检测插件uptodate的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter应用更新检测插件uptodate的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


uptodate 是一个用于 Flutter 应用的插件,用于检测应用是否有可用的更新。它可以帮助开发者轻松地在应用中集成更新检查功能,并提示用户进行更新。以下是使用 uptodate 插件的详细步骤:

1. 添加依赖

首先,在 pubspec.yaml 文件中添加 uptodate 插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  uptodate: ^1.0.0  # 请根据实际情况使用最新版本

然后,运行 flutter pub get 来安装依赖。

2. 初始化插件

在你的 Dart 代码中导入 uptodate 插件并初始化它。通常在 main.dart 文件中的 main 函数中初始化:

import 'package:flutter/material.dart';
import 'package:uptodate/uptodate.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  
  // 初始化 uptodate 插件
  await UpToDate.initialize(
    appId: 'YOUR_APP_ID',  // 替换为你的应用 ID
    appStoreUrl: 'https://apps.apple.com/app/idYOUR_APP_ID',  // App Store URL
    playStoreUrl: 'market://details?id=YOUR_PACKAGE_NAME',  // Play Store URL
  );

  runApp(MyApp());
}

3. 检查更新

在应用的某个地方(例如在主页面的 initState 中),调用 UpToDate.checkForUpdate 方法来检查是否有更新:

import 'package:flutter/material.dart';
import 'package:uptodate/uptodate.dart';

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  void initState() {
    super.initState();
    _checkForUpdate();
  }

  Future<void> _checkForUpdate() async {
    final updateAvailable = await UpToDate.checkForUpdate();
    if (updateAvailable) {
      _showUpdateDialog();
    }
  }

  void _showUpdateDialog() {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('Update Available'),
          content: Text('A new version of the app is available. Please update to the latest version.'),
          actions: <Widget>[
            TextButton(
              child: Text('Update'),
              onPressed: () {
                UpToDate.launchStore();
                Navigator.of(context).pop();
              },
            ),
            TextButton(
              child: Text('Later'),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
          ],
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('UpToDate Example'),
      ),
      body: Center(
        child: Text('Check for updates on app launch'),
      ),
    );
  }
}

4. 处理更新提示

在上面的代码中,_showUpdateDialog 方法会显示一个对话框,提示用户有可用的更新。用户可以选择立即更新或稍后更新。如果用户选择更新,UpToDate.launchStore() 将打开应用商店页面。

5. 其他配置

uptodate 插件还提供了其他一些配置选项,例如:

  • minAppVersion: 设置最小应用版本,确保用户更新到特定版本。
  • ignoredVersions: 忽略某些版本,不提示更新。
  • forceUpdate: 强制用户更新,不允许跳过。

你可以在 UpToDate.initialize 方法中使用这些选项:

await UpToDate.initialize(
  appId: 'YOUR_APP_ID',
  appStoreUrl: 'https://apps.apple.com/app/idYOUR_APP_ID',
  playStoreUrl: 'market://details?id=YOUR_PACKAGE_NAME',
  minAppVersion: '1.0.1',  // 设置最小应用版本
  ignoredVersions: ['1.0.0'],  // 忽略特定版本
  forceUpdate: true,  // 强制更新
);
回到顶部