Skip to content

Instantly share code, notes, and snippets.

@RobertApikyan
Created March 19, 2024 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RobertApikyan/76850fdcc172b7ed3136d24cf44d9ad3 to your computer and use it in GitHub Desktop.
Save RobertApikyan/76850fdcc172b7ed3136d24cf44d9ad3 to your computer and use it in GitHub Desktop.
import 'package:shared_preferences/shared_preferences.dart';
import 'package:uuid/uuid.dart';
class InstanceId {
InstanceId._();
factory InstanceId() => _instance ?? (_instance = InstanceId._());
static InstanceId? _instance;
static const String _key = 'InstanceId';
String? _value;
Future<String> get() async {
if (_value != null) {
return _value!;
}
final prefs = await SharedPreferences.getInstance();
String storedId = prefs.getString(_key) ?? '';
if (storedId.trim().isEmpty) {
storedId = const Uuid().v1();
await prefs.setString(_key, storedId);
}
return _value = storedId;
}
Future<void> dispose() async {
final prefs = await SharedPreferences.getInstance();
await prefs.remove(_key);
_value = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment