diff --git a/packages/altfire_configurator/lib/src/config.dart b/packages/altfire_configurator/lib/src/config.dart index a198e31..8e58ff3 100644 --- a/packages/altfire_configurator/lib/src/config.dart +++ b/packages/altfire_configurator/lib/src/config.dart @@ -5,12 +5,12 @@ import 'dart:async'; class Config { Config({ required T value, - required StreamSubscription subscription, + required StreamSubscription? subscription, }) : _value = value, _subscription = subscription; /// The subscription to the stream of updated parameter information. - final StreamSubscription _subscription; + final StreamSubscription? _subscription; /// The current value of the parameter. T get value => _value; @@ -18,6 +18,6 @@ class Config { /// Closes the [Config]. Future dispose() async { - await _subscription.cancel(); + await _subscription?.cancel(); } } diff --git a/packages/altfire_configurator/lib/src/configurator.dart b/packages/altfire_configurator/lib/src/configurator.dart index 76fe00a..5225555 100644 --- a/packages/altfire_configurator/lib/src/configurator.dart +++ b/packages/altfire_configurator/lib/src/configurator.dart @@ -1,11 +1,11 @@ import 'dart:convert'; import 'package:firebase_remote_config/firebase_remote_config.dart'; -import 'package:meta/meta.dart'; +import 'package:flutter/foundation.dart'; import 'config.dart'; -typedef ValueChanged = void Function(T value); +typedef _ValueChanged = void Function(T value); /// A class that wraps Remote Config. /// Its role is to "fetch the configured parameters from remote and provide @@ -13,6 +13,7 @@ typedef ValueChanged = void Function(T value); /// /// Exposes [fetchAndActivate] and configuration methods for Remote Config. class Configurator { + /// If [rc] is null, a singleton instance of [FirebaseRemoteConfig] is used. Configurator({ FirebaseRemoteConfig? rc, }) : _rc = rc ?? FirebaseRemoteConfig.instance; @@ -116,84 +117,96 @@ class Configurator { /// Returns a [Config] of type [String]. Config getStringConfig( String key, { - required ValueChanged onConfigUpdated, + _ValueChanged? onConfigUpdated, }) { return Config( value: getString(key), - subscription: filteredOnConfigUpdated(key).listen((event) async { - await activate(); - onConfigUpdated(getString(key)); - }), + subscription: kIsWeb || onConfigUpdated == null + ? null + : filteredOnConfigUpdated(key).listen((event) async { + await activate(); + onConfigUpdated(getString(key)); + }), ); } /// Returns a [Config] of type [int]. Config getIntConfig( String key, { - required ValueChanged onConfigUpdated, + _ValueChanged? onConfigUpdated, }) { return Config( value: getInt(key), - subscription: filteredOnConfigUpdated(key).listen((event) async { - await activate(); - onConfigUpdated(getInt(key)); - }), + subscription: kIsWeb || onConfigUpdated == null + ? null + : filteredOnConfigUpdated(key).listen((event) async { + await activate(); + onConfigUpdated(getInt(key)); + }), ); } /// Returns a [Config] of type [double]. Config getDoubleConfig( String key, { - required ValueChanged onConfigUpdated, + _ValueChanged? onConfigUpdated, }) { return Config( value: getDouble(key), - subscription: filteredOnConfigUpdated(key).listen((event) async { - await activate(); - onConfigUpdated(getDouble(key)); - }), + subscription: kIsWeb || onConfigUpdated == null + ? null + : filteredOnConfigUpdated(key).listen((event) async { + await activate(); + onConfigUpdated(getDouble(key)); + }), ); } /// Returns a [Config] of type [bool]. Config getBoolConfig( String key, { - required ValueChanged onConfigUpdated, + _ValueChanged? onConfigUpdated, }) { return Config( value: getBool(key), - subscription: filteredOnConfigUpdated(key).listen((event) async { - await activate(); - onConfigUpdated(getBool(key)); - }), + subscription: kIsWeb || onConfigUpdated == null + ? null + : filteredOnConfigUpdated(key).listen((event) async { + await activate(); + onConfigUpdated(getBool(key)); + }), ); } /// Returns a [Config] of type [Map]. Config> getJsonConfig( String key, { - required ValueChanged> onConfigUpdated, + _ValueChanged>? onConfigUpdated, }) { return Config>( value: getJson(key), - subscription: filteredOnConfigUpdated(key).listen((event) async { - await activate(); - onConfigUpdated(getJson(key)); - }), + subscription: kIsWeb || onConfigUpdated == null + ? null + : filteredOnConfigUpdated(key).listen((event) async { + await activate(); + onConfigUpdated(getJson(key)); + }), ); } /// Returns a [Config] of type [List] of [Map]. Config>> getListJsonConfig( String key, { - required ValueChanged>> onConfigUpdated, + _ValueChanged>>? onConfigUpdated, }) { return Config>>( value: getListJson(key), - subscription: filteredOnConfigUpdated(key).listen((event) async { - await activate(); - onConfigUpdated(getListJson(key)); - }), + subscription: kIsWeb || onConfigUpdated == null + ? null + : filteredOnConfigUpdated(key).listen((event) async { + await activate(); + onConfigUpdated(getListJson(key)); + }), ); } @@ -201,14 +214,16 @@ class Configurator { Config getDataConfig( String key, { required T Function(Map) fromJson, - required ValueChanged onConfigUpdated, + _ValueChanged? onConfigUpdated, }) { return Config( value: getData(key: key, fromJson: fromJson), - subscription: filteredOnConfigUpdated(key).listen((event) async { - await activate(); - onConfigUpdated(getData(key: key, fromJson: fromJson)); - }), + subscription: kIsWeb || onConfigUpdated == null + ? null + : filteredOnConfigUpdated(key).listen((event) async { + await activate(); + onConfigUpdated(getData(key: key, fromJson: fromJson)); + }), ); } } diff --git a/packages/altfire_configurator/pubspec.yaml b/packages/altfire_configurator/pubspec.yaml index 9f00df9..be7b339 100644 --- a/packages/altfire_configurator/pubspec.yaml +++ b/packages/altfire_configurator/pubspec.yaml @@ -14,6 +14,8 @@ environment: dependencies: firebase_remote_config: ^5.1.3 + flutter: + sdk: flutter meta: ^1.12.0 dev_dependencies: