setRemoteId static method

Future<void> setRemoteId(
  1. String remoteId
)

设置remoteId并持久化 当remoteId为空时,不保存到本地存储,保持原有值不变

Implementation

static Future<void> setRemoteId(String remoteId) async {
  // 确保已初始化
  await _ensureInitialized();

  try {
    // 只有当remoteId不为空时才保存到本地存储
    if (remoteId.isNotEmpty) {
      final prefs = await SharedPreferences.getInstance();
      await prefs.setString(_remoteIdKey, remoteId);
      _remoteId = remoteId;
      log('保存remoteId到本地存储: $_remoteId');
    } else {
      // 空值时不更新内存中的值,不覆盖本地存储
      log('remoteId为空,忽略保存,保持本地存储的值不变');
    }
  } catch (e) {
    log('保存remoteId失败: $e');
    // 即使保存失败,也更新内存中的值(如果不为空)
    if (remoteId.isNotEmpty) {
      _remoteId = remoteId;
    }
  }
}