build method

  1. @override
Future<Profile> build()

Implementation

@override
Future<Profile> build() async {
  final user = ref.watch(currentUserProvider);
  if (user == null) {
    throw StateError('User not authenticated');
  }

  // Native stale-while-revalidate: the local cache races the network fetch.
  // The network result overwrites silently; if it wins first, Riverpod's
  // didChange guard discards the slower cached value.
  if (persistentApiCacheEnabled) {
    persist(
      ref.watch(storageProvider.future),
      key: profileCacheKey(user.id),
      options: const StorageOptions(
        cacheTime: StorageCacheTime.unsafe_forever,
        // Cache-schema version for the persisted profile. Bumped to 2 when
        // the hand-written UserProfile was replaced by generated Profile.
        destroyKey: '2',
      ),
    );
  }

  return ref.watch(profileRepositoryProvider).getProfile();
}