47 lines
1.4 KiB
Dart
47 lines
1.4 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:package_info_plus/package_info_plus.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
|
|
|
class Env {
|
|
static String get apiUrl {
|
|
final backendUrl = const String.fromEnvironment('BACKEND_URL');
|
|
if (backendUrl.isNotEmpty && backendUrl != 'false') {
|
|
return backendUrl;
|
|
}
|
|
if (Platform.isAndroid) {
|
|
return 'http://10.0.2.2:5775';
|
|
}
|
|
return 'http://localhost:5775';
|
|
}
|
|
|
|
static String version = '0.1.0';
|
|
static int build = 1;
|
|
static String deviceId = '';
|
|
|
|
static Future<void> init() async {
|
|
final info = await PackageInfo.fromPlatform();
|
|
version = info.version;
|
|
final buildStr = info.buildNumber.isEmpty ? '1' : info.buildNumber;
|
|
build = int.tryParse(buildStr) ?? 1;
|
|
|
|
deviceId = await _getOrCreateDeviceId();
|
|
}
|
|
|
|
static Future<String> _getOrCreateDeviceId() async {
|
|
const storage = FlutterSecureStorage();
|
|
var deviceId = await storage.read(key: 'device_id');
|
|
if (deviceId == null || deviceId.isEmpty) {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
deviceId = prefs.getString('device_id') ?? '';
|
|
if (deviceId.isEmpty) {
|
|
deviceId = 'install_${DateTime.now().millisecondsSinceEpoch}';
|
|
await prefs.setString('device_id', deviceId);
|
|
}
|
|
await storage.write(key: 'device_id', value: deviceId);
|
|
}
|
|
return deviceId;
|
|
}
|
|
}
|