Created
March 19, 2024 09:09
-
-
Save RobertApikyan/76850fdcc172b7ed3136d24cf44d9ad3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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