30 lines
946 B
Dart
30 lines
946 B
Dart
import 'package:analytics_sdk/utils/platform_storage.dart';
|
|
import 'package:analytics_sdk/utils/uuid_util.dart';
|
|
import 'package:flutter/services.dart';
|
|
|
|
const _kDeviceIdStorageKey = 'analytics_device_id.txt';
|
|
const _kNativeChannel = MethodChannel('analytics_sdk');
|
|
|
|
Future<String> generateOrLoadDeviceId() async {
|
|
try {
|
|
final nativeDeviceId =
|
|
(await _kNativeChannel.invokeMethod<String>('getDeviceId'))?.trim() ??
|
|
'';
|
|
if (nativeDeviceId.isNotEmpty) return nativeDeviceId;
|
|
} on MissingPluginException {
|
|
// Desktop platforms may not implement this method; fall back to storage.
|
|
} catch (_) {}
|
|
|
|
try {
|
|
final existing =
|
|
(await PlatformStorage.read(_kDeviceIdStorageKey))?.trim() ?? '';
|
|
if (existing.isNotEmpty) return existing;
|
|
|
|
final generated = generateUuidV4();
|
|
await PlatformStorage.write(_kDeviceIdStorageKey, generated);
|
|
return generated;
|
|
} catch (_) {
|
|
return '';
|
|
}
|
|
}
|