简体   繁体   中英

Accessing RemoteConfig create loop Flutter Web

I'm using firebase: ^7.3.0 package to use firebase services that are not yet available for web. Auth, RTD, and Storage are working great and I'm now trying to use RemoteConfig and Messaging(not testes yet) but I have a problem instantiating RemoteConfig. Looking at the firebase: ^7.3.0 package code, I see that one file is basically a singleton itself, for example App and RemoteConfig part is:

App app([String name]) {
  var jsObject = (name != null) ? firebase.app(name) : firebase.app();

  return App.getInstance(jsObject);
}

App initializeApp(
    {String apiKey,
    String authDomain,
    String databaseURL,
    String projectId,
    String storageBucket,
    String messagingSenderId,
    String name,
    String measurementId,
    String appId}) {
  name ??= _defaultAppName;

  try {
    return App.getInstance(firebase.initializeApp(
        firebase.FirebaseOptions(
            apiKey: apiKey,
            authDomain: authDomain,
            databaseURL: databaseURL,
            projectId: projectId,
            storageBucket: storageBucket,
            messagingSenderId: messagingSenderId,
            measurementId: measurementId,
            appId: appId),
        name));
  } catch (e) {
    if (_firebaseNotLoaded(e)) {
      throw FirebaseJsNotLoadedException('firebase.js must be loaded.');
    }

    rethrow;
  }
}

RemoteConfig remoteConfig([App app]) {
  var jsObject = (app != null)
      ? firebase.remoteConfig(app.jsObject)
      : firebase.remoteConfig();

  return RemoteConfig.getInstance(jsObject);
}

so I initialize Firebase app in my own singleton as:

class FirebaseWeb {
  static final FirebaseWeb _singleton = FirebaseWeb._();

  static FirebaseWeb get instance => _singleton;
  FirebaseWeb._();

  static App _app;
  static Messaging _messaging;
  static RemoteConfig _remoteConfig;

  RemoteConfig get remoteConfig {
    print(('FirebaseWeb get remoteConfig called'));
    if (_app != null) {
      _remoteConfig = remoteConfig;
      return _remoteConfig;
    } else {
      print('initialize app');
      _app = initializeApp(
          apiKey: "myValue",
          authDomain: "myValue",
          databaseURL: "myValue",
          projectId: "myValue",
          storageBucket: "myValue",
          messagingSenderId: "myValue",
          appId: "myValue");
      print('initialized app is $_app');
      _remoteConfig = remoteConfig;
      return _remoteConfig;
    }
  }

  Messaging get messaging {
    print(('FirebaseWeb get messaging called'));
    if (_app != null) {
      return _messaging;
    } else {
      print('initialize app');
      _app = initializeApp(
          apiKey: "myValue",
          authDomain: "myValue",
          databaseURL: "myValue",
          projectId: "myValue",
          storageBucket: "myValue",
          messagingSenderId: "myValue",
          appId: "myValue");
      print('initialized app is $_app');
      return _messaging;
    }
  }
  // Database object accessor

  App get app {
    print('FirebaseWeb get app called ');
    print('_app is $_app');
    if (_app != null) {
      return _app;
    } else {
      print('initialize app');
      _app = initializeApp(
          apiKey: "myValue",
          authDomain: "myValue",
          databaseURL: "myValue",
          projectId: "myValue",
          storageBucket: "myValue",
          messagingSenderId: "myValue",
          appId: "myValue");
      print('initialized app is $_app');
      return _app;
    }
  }
}

Then for accessing Auth, Realtime Database and Storage it works perfectly instantiating it and using it as:

App firebase = FirebaseWeb.instance.app;
firebase.database()
firebase.storage()
firebase.auth()

The problem is that when instantiating RemoteConfig as

RemoteConfig remoteConfig = FirebaseWeb.instance.remoteConfig;

in Chrome console get the print FirebaseWeb get remoteConfig called over 7k times..

What am I doing wrong initializing App and Remote config? Many thanks

Solved.. there is no need to initialize anything else than App() in the singleton.. Just init firebase App firebase = FirebaseWeb.instance.app; in the classes you need to use remoteConfig and it's is then available to use right away.. guess the same is valid for Messaging..

App firebase = FirebaseWeb.instance.app;

  @override
  Future<void> getRemoteValues() async {
    print(
        'PlatformRemoteConfigWeb.getRemoteValues() started');
    remoteConfig().ensureInitialized().catchError((e) {
      print(
          'PlatformRemoteConfigWeb.getRemoteValues()'
          'initialize error: $e');
    });

    remoteConfig().setLogLevel(RemoteConfigLogLevel.debug);

    Map<String, dynamic> defaultsMap = {
      'localization_version': '0',
      'remoteItemsCategoryArray': ''
    };
    remoteConfig().defaultConfig = defaultsMap;

    try {
      await remoteConfig().fetch().timeout(Duration(seconds: 2));

      await remoteConfig().activate();

      int localizationVersion =
          remoteConfig().getNumber('localization_version');

      String remoteItemsCategoryArray =
          remoteConfig().getString('remoteItemsCategoryList');

      print(
          'PlatformRemoteConfigWeb.getRemoteValues() values are : $localizationVersion, and $remoteItemsCategoryArray'); //, and $remoteWorkshopCategoryArray');

      RemoteValues values = RemoteValues(
          version: localizationVersion,
          itemsCategoriesList: remoteItemsCategoryArray); 

      return values;
    } catch (exception) {
      print(
          'PlatformRemoteConfigWeb.getRemoteValues() Unable to fetch remote config. Cached or default values will be '
          'used. Error: $exception');
    }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM