Commit efe7c8be by tanghuan

App版本号改为从 pubspec.yaml 中读取

1 parent 17d6557f
...@@ -50,7 +50,11 @@ class Constant { ...@@ -50,7 +50,11 @@ class Constant {
/// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// ///
/// app 版本号规则 /// app 版本号规则
static const String appVersion = EnvConfig.version; ///
/// 实际取值来源于 pubspec.yaml 的 version 字段,
/// 在 main.dart 中通过 package_info_plus 读取后赋值;
/// EnvConfig.version 仅作为读取失败时的兜底默认值。
static String appVersion = EnvConfig.version;
/// 当前的H5业务包的版本 /// 当前的H5业务包的版本
/// 从 key=h5VersionKey 的缓存中获取,获取不到时值时,说明是第一次打开APP,则从基础业务包中读取的。 /// 从 key=h5VersionKey 的缓存中获取,获取不到时值时,说明是第一次打开APP,则从基础业务包中读取的。
......
...@@ -8,6 +8,7 @@ import 'package:appframe/ui/widgets/ios_edge_swipe_detector.dart'; ...@@ -8,6 +8,7 @@ import 'package:appframe/ui/widgets/ios_edge_swipe_detector.dart';
import 'package:archive/archive.dart'; import 'package:archive/archive.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import 'app.dart'; import 'app.dart';
...@@ -16,12 +17,28 @@ void main() async { ...@@ -16,12 +17,28 @@ void main() async {
WidgetsFlutterBinding.ensureInitialized(); WidgetsFlutterBinding.ensureInitialized();
await setupLocator(); await setupLocator();
await _initAppVersion();
await _initH5Version(); await _initH5Version();
await _initIOSGesture(); await _initIOSGesture();
runApp(const App()); runApp(const App());
} }
/// 从 pubspec.yaml 中读取并初始化 APP 版本号
///
/// pubspec.yaml 中的 version 字段作为全局唯一数据源,
/// 避免与构建参数、硬编码出现不一致。
Future<void> _initAppVersion() async {
try {
final info = await PackageInfo.fromPlatform();
if (info.version.isNotEmpty) {
Constant.appVersion = info.version;
}
} catch (e) {
debugPrint('读取 APP 版本号失败,使用默认值: $e');
}
}
Future<void> _initH5Version() async { Future<void> _initH5Version() async {
// 1 读取保存的H5版本 // 1 读取保存的H5版本
var h5Version = getIt.get<SharedPreferences>().getString(Constant.h5VersionKey); var h5Version = getIt.get<SharedPreferences>().getString(Constant.h5VersionKey);
......
...@@ -18,6 +18,7 @@ dependencies: ...@@ -18,6 +18,7 @@ dependencies:
archive: ^4.0.7 archive: ^4.0.7
connectivity_plus: ^7.0.0 connectivity_plus: ^7.0.0
device_info_plus: ^11.5.0 device_info_plus: ^11.5.0
package_info_plus: ^8.3.1
dio: ^5.9.0 dio: ^5.9.0
equatable: ^2.0.7 equatable: ^2.0.7
get_it: ^8.2.0 get_it: ^8.2.0
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!