Skip to content

Instantly share code, notes, and snippets.

@bizz84
Created April 29, 2025 13:19
Show Gist options
  • Save bizz84/5bdbc7685564e43c9cdfe5b658248f4b to your computer and use it in GitHub Desktop.
Save bizz84/5bdbc7685564e43c9cdfe5b658248f4b to your computer and use it in GitHub Desktop.
Flutter Remote Config boilerplate code (using Riverpod)
import 'dart:async';
import 'dart:developer';
import 'package:firebase_remote_config/firebase_remote_config.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'firebase_remote_config_provider.g.dart';
@Riverpod(keepAlive: true)
Future<FirebaseRemoteConfig> firebaseRemoteConfig(Ref ref) async {
// Fetch up to 5 times per hour
// https://firebase.google.com/docs/remote-config/get-started?platform=ios#throttling
final minimumFetchIntervalMinutes = appFlavor == 'prod' ? 12 : 1;
final remoteConfig = FirebaseRemoteConfig.instance;
await remoteConfig.setConfigSettings(RemoteConfigSettings(
fetchTimeout: const Duration(minutes: 1),
minimumFetchInterval: Duration(minutes: minimumFetchIntervalMinutes),
));
// Set default values (useful if app is offline on first launch)
await remoteConfig.setDefaults({
'required_version': '0.1.0',
});
// Load new values for next startup
// https://firebase.google.com/docs/remote-config/loading#strategy_3_load_new_values_for_next_startup
// 1. Immediately activate previously fetched values
await remoteConfig.activate();
// 2. Fetch new values (unawaited)
unawaited(remoteConfig.fetch());
StreamSubscription<RemoteConfigUpdate>? sub;
if (!kIsWeb) {
// 3. Add a real-time config update listener (non-web only)
sub = remoteConfig.onConfigUpdated.listen((event) {
// 4. Just log the updated keys. The values will be activated next time the app starts.
log('Updated keys: ${event.updatedKeys}', name: 'Remote Config');
});
}
// Don't forget to cancel the subscription
if (sub != null) {
ref.onDispose(sub.cancel);
}
return remoteConfig;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment