|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import 'package:firebase_admin_sdk/firebase_admin_sdk.dart'; |
| 16 | +import 'package:firebase_admin_sdk/remote_config.dart'; |
| 17 | + |
| 18 | +Future<void> remoteConfigExample(FirebaseApp admin) async { |
| 19 | + print('\n### Remote Config Example ###\n'); |
| 20 | + |
| 21 | + final remoteConfig = admin.remoteConfig(); |
| 22 | + |
| 23 | + // Example 1: Read the active template. |
| 24 | + RemoteConfigTemplate? template; |
| 25 | + try { |
| 26 | + print('> Fetching active template...\n'); |
| 27 | + template = await remoteConfig.getTemplate(); |
| 28 | + print('Template fetched!'); |
| 29 | + print(' - ETag: ${template.etag}'); |
| 30 | + print(' - Conditions: ${template.conditions.length}'); |
| 31 | + print(' - Parameters: ${template.parameters.length}'); |
| 32 | + print(' - Parameter groups: ${template.parameterGroups.length}'); |
| 33 | + if (template.version != null) { |
| 34 | + print(' - Version: ${template.version!.versionNumber}'); |
| 35 | + } |
| 36 | + print(''); |
| 37 | + } on FirebaseRemoteConfigException catch (e) { |
| 38 | + print('> Remote Config error: ${e.code} - ${e.message}'); |
| 39 | + return; |
| 40 | + } catch (e) { |
| 41 | + print('> Error fetching template: $e'); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + // Example 2: Validate a modified template without publishing. |
| 46 | + try { |
| 47 | + print('> Validating a modified template (no publish)...\n'); |
| 48 | + final modified = RemoteConfigTemplate( |
| 49 | + etag: template.etag, |
| 50 | + conditions: template.conditions, |
| 51 | + parameters: <String, RemoteConfigParameter>{ |
| 52 | + ...template.parameters, |
| 53 | + 'dart_admin_sdk_demo': RemoteConfigParameter( |
| 54 | + defaultValue: const ExplicitParameterValue(value: 'hello'), |
| 55 | + description: 'Demo parameter from the Dart Admin SDK example.', |
| 56 | + valueType: ParameterValueType.string, |
| 57 | + ), |
| 58 | + }, |
| 59 | + parameterGroups: template.parameterGroups, |
| 60 | + version: template.version, |
| 61 | + ); |
| 62 | + final validated = await remoteConfig.validateTemplate(modified); |
| 63 | + print('Template validated!'); |
| 64 | + print(' - ETag (restored): ${validated.etag}'); |
| 65 | + print(''); |
| 66 | + } on FirebaseRemoteConfigException catch (e) { |
| 67 | + print('> Remote Config error: ${e.code} - ${e.message}'); |
| 68 | + } catch (e) { |
| 69 | + print('> Error validating template: $e'); |
| 70 | + } |
| 71 | + |
| 72 | + // Example 3: List published versions. |
| 73 | + try { |
| 74 | + print('> Listing published versions (page size 5)...\n'); |
| 75 | + final result = await remoteConfig.listVersions( |
| 76 | + ListVersionsOptions(pageSize: 5), |
| 77 | + ); |
| 78 | + print('Got ${result.versions.length} version(s):'); |
| 79 | + for (final v in result.versions) { |
| 80 | + print(' - v${v.versionNumber}: ${v.description ?? '(no description)'}'); |
| 81 | + } |
| 82 | + print(''); |
| 83 | + } on FirebaseRemoteConfigException catch (e) { |
| 84 | + print('> Remote Config error: ${e.code} - ${e.message}'); |
| 85 | + } catch (e) { |
| 86 | + print('> Error listing versions: $e'); |
| 87 | + } |
| 88 | + |
| 89 | + // Example 4: Server-side template evaluation. |
| 90 | + try { |
| 91 | + print('> Fetching server template and evaluating...\n'); |
| 92 | + final serverTemplate = await remoteConfig.getServerTemplate( |
| 93 | + defaultConfig: const <String, Object>{ |
| 94 | + 'enable_new_ui': false, |
| 95 | + 'max_items': 50, |
| 96 | + }, |
| 97 | + ); |
| 98 | + final config = serverTemplate.evaluate( |
| 99 | + const EvaluationContext( |
| 100 | + randomizationId: 'demo-user-id', |
| 101 | + customSignals: <String, Object>{ |
| 102 | + 'app_version': '2.3.1', |
| 103 | + 'country': 'US', |
| 104 | + }, |
| 105 | + ), |
| 106 | + ); |
| 107 | + print('Server config evaluated!'); |
| 108 | + print(' - enable_new_ui: ${config.getBoolean('enable_new_ui')}'); |
| 109 | + print(' - max_items: ${config.getInt('max_items')}'); |
| 110 | + final all = config.getAll(); |
| 111 | + print(' - total resolved keys: ${all.length}'); |
| 112 | + print(''); |
| 113 | + } on FirebaseRemoteConfigException catch (e) { |
| 114 | + print('> Remote Config error: ${e.code} - ${e.message}'); |
| 115 | + } catch (e) { |
| 116 | + print('> Error evaluating server template: $e'); |
| 117 | + } |
| 118 | +} |
0 commit comments