Repository: localsend/localsend
File: lib/provider/persistence_provider.dart (Dart)

Code before:

  PersistenceService._(this._prefs);

  static Future<PersistenceService> initialize() async {
    final prefs = await SharedPreferences.getInstance();

    if (prefs.getInt(_version) == null) {
      await prefs.setInt(_version, 1);
    }

    if (prefs.getString(_showToken) == null) {
      await prefs.setString(_showToken, const Uuid().v4());
    }

    if (prefs.getString(_aliasKey) == null) {
      await prefs.setString(_aliasKey, generateRandomAlias());
    }

    if (prefs.getString(_themeKey) == null) {
      await prefs.setString(_themeKey, ThemeMode.system.name);
    }

    return PersistenceService._(prefs);
  }

  static Future<PersistenceService> getPersistence() async {
    final prefs = await SharedPreferences.getInstance();
    return PersistenceService._(prefs);
  }

  List<ReceiveHistoryEntry> getReceiveHistory() {
    final historyRaw = _prefs.getStringList(_receiveHistory) ?? [];
    return historyRaw.map((entry) => ReceiveHistoryEntry.fromJson(jsonDecode(entry))).toList();
  }

  Future<void> setReceiveHistory(List<ReceiveHistoryEntry> entries) async {
    final historyRaw = entries.map((entry) => jsonEncode(entry.toJson())).toList();
    await _prefs.setStringList(_receiveHistory, historyRaw);
  }

  String getShowToken() {
    return _prefs.getString(_showToken)!;
  }

  String getAlias() {
    return _prefs.getString(_aliasKey) ?? generateRandomAlias();
  }

  Future<void> setAlias(String alias) async {
    await _prefs.setString(_aliasKey, alias);
  }

Review comment (security):
`riverpod` is used as the dependency injection tool so you can get the instance by calling

`final persistenceService = ref.watch(persistenceProvider);`

The widget must be a `ConsumerWidget` / `ConsumerStatefulWidget` to have access to `ref` though. So either you can change the `WindowWatcher` from a `StatefulWidget` to a `ConsumerStatefulWidget` or you can get the `persistenceService` inside `main.dart` and inject it into `WindowWatcher` via constructor.

Code after:
  final SharedPreferences _prefs;

  PersistenceService._(this._prefs);

  static Future<PersistenceService> initialize() async {
    final prefs = await SharedPreferences.getInstance();

    if (prefs.getInt(_version) == null) {
      await prefs.setInt(_version, 1);
    }

    if (prefs.getString(_showToken) == null) {
      await prefs.setString(_showToken, const Uuid().v4());
    }

    if (prefs.getString(_aliasKey) == null) {
      await prefs.setString(_aliasKey, generateRandomAlias());
    }

    if (prefs.getString(_themeKey) == null) {
      await prefs.setString(_themeKey, ThemeMode.system.name);
    }

    return PersistenceService._(prefs);
  }

  List<ReceiveHistoryEntry> getReceiveHistory() {
    final historyRaw = _prefs.getStringList(_receiveHistory) ?? [];
    return historyRaw.map((entry) => ReceiveHistoryEntry.fromJson(jsonDecode(entry))).toList();
  }

  Future<void> setReceiveHistory(List<ReceiveHistoryEntry> entries) async {
    final historyRaw = entries.map((entry) => jsonEncode(entry.toJson())).toList();
    await _prefs.setStringList(_receiveHistory, historyRaw);
  }

  String getShowToken() {
    return _prefs.getString(_showToken)!;
  }

  String getAlias() {
    return _prefs.getString(_aliasKey) ?? generateRandomAlias();
  }

  Future<void> setAlias(String alias) async {
    await _prefs.setString(_aliasKey, alias);
  }

  ThemeMode getTheme() {
    final value = _prefs.getString(_themeKey);
    if (value == null) {