From dec3c6e789c2eedf90d2ad0f819783497424c588 Mon Sep 17 00:00:00 2001 From: Pawan Date: Thu, 25 Mar 2021 14:41:38 +0530 Subject: [PATCH 1/2] Firebase Remote Config --- lib/services/remote_config.dart | 47 +++++++++++++++++++++++++++++++++ pubspec.yaml | 1 + 2 files changed, 48 insertions(+) create mode 100644 lib/services/remote_config.dart diff --git a/lib/services/remote_config.dart b/lib/services/remote_config.dart new file mode 100644 index 0000000..df1ef2d --- /dev/null +++ b/lib/services/remote_config.dart @@ -0,0 +1,47 @@ +import 'package:firebase_core/firebase_core.dart'; +import 'package:firebase_remote_config/firebase_remote_config.dart'; +import 'package:flutter/material.dart'; + +const String _BOOLEAN_VALUE="sample_bool_value"; +const String _INT_VALUE="sample_int_value"; +const String _STRING_VALUE="sample_string_value"; +class RemoteConfigService { + final RemoteConfig _remoteConfig; + RemoteConfigService({RemoteConfig remoteConfig }) :_remoteConfig=remoteConfig; + final defaults={ + _BOOLEAN_VALUE:false, + _INT_VALUE:01, + _STRING_VALUE:"Remote Config Sample", + }; + + static RemoteConfigService _instance; + static Future getInstance() async { + if(_instance==null){ + _instance=RemoteConfigService( + remoteConfig: await RemoteConfig.instance, + ); + } + return _instance; + } + + bool get getBoolValue=>_remoteConfig.getBool(_BOOLEAN_VALUE); + bool get getIntValue=>_remoteConfig.getBool(_INT_VALUE); + bool get getStringValue=>_remoteConfig.getBool(_STRING_VALUE); + + Future initialize() async { + try { + await _remoteConfig.setDefaults(defaults); + await _fetchAndActivate(); + } on FetchThrottledException catch (e) { + print("Remote config fetch throttled : $e"); + } catch (e){ + print("Unable to fecth"); + } + } + + Future _fetchAndActivate() async { + await _remoteConfig.fetch(expiration: Duration(hours: 4)); + await _remoteConfig.activateFetched(); + } + +} \ No newline at end of file diff --git a/pubspec.yaml b/pubspec.yaml index fabe1a2..26ab927 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -25,6 +25,7 @@ dependencies: sdk: flutter fluttertoast: ^7.1.8 + firebase_remote_config: ^0.6.0 firebase_core: 0.7.0 google_fonts: ^1.1.0 razorpay_flutter: 1.1.0 From 6f74f0b36bacb90b822bbfb2f22aa6c91d6d4a0f Mon Sep 17 00:00:00 2001 From: Pawan Date: Sat, 27 Mar 2021 02:29:16 +0530 Subject: [PATCH 2/2] Initialized remote config main --- lib/main.dart | 9 +++++++++ lib/services/remote_config.dart | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 190f46f..0b1b6af 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,19 +1,28 @@ import 'package:firebase_core/firebase_core.dart'; +import 'package:firebase_remote_config/firebase_remote_config.dart'; import 'package:flutter/material.dart'; import 'package:google_fonts/google_fonts.dart'; import 'package:provider/provider.dart'; +import 'services/remote_config.dart'; import 'package:retro_shopping/helpers/ad_state.dart'; import 'package:retro_shopping/helpers/constants.dart'; import 'package:google_mobile_ads/google_mobile_ads.dart'; import 'package:retro_shopping/helpers/route_page.dart'; +RemoteConfigService _remoteConfigService; Future main() async { + //firebase Initialization WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); //AdMob Initialization final Future initFuture = MobileAds.instance.initialize(); + + //Initialize remote config + _remoteConfigService= await RemoteConfigService.getInstance(); + await _remoteConfigService.initialize(); + final AdState adState = AdState(initFuture); runApp( diff --git a/lib/services/remote_config.dart b/lib/services/remote_config.dart index df1ef2d..eaf44f6 100644 --- a/lib/services/remote_config.dart +++ b/lib/services/remote_config.dart @@ -25,8 +25,8 @@ class RemoteConfigService { } bool get getBoolValue=>_remoteConfig.getBool(_BOOLEAN_VALUE); - bool get getIntValue=>_remoteConfig.getBool(_INT_VALUE); - bool get getStringValue=>_remoteConfig.getBool(_STRING_VALUE); + int get getIntValue=>_remoteConfig.getInt(_INT_VALUE); + String get getStringValue=>_remoteConfig.getString(_STRING_VALUE); Future initialize() async { try {