tasq/lib/utils/device_id.dart

24 lines
697 B
Dart

import 'package:shared_preferences/shared_preferences.dart';
import 'package:uuid/uuid.dart';
class DeviceId {
static const _key = 'tasq_device_id';
/// Returns a stable UUID for this installation. Generated once and persisted
/// in `SharedPreferences`.
static Future<String> getId() async {
final prefs = await SharedPreferences.getInstance();
var id = prefs.getString(_key);
if (id != null && id.isNotEmpty) return id;
id = const Uuid().v4();
await prefs.setString(_key, id);
return id;
}
/// For testing or explicit reset.
static Future<void> reset() async {
final prefs = await SharedPreferences.getInstance();
await prefs.remove(_key);
}
}