访问 RemoteConfig 创建循环 Flutter Web

Accessing RemoteConfig create loop Flutter Web

我正在使用 firebase: ^7.3.0 软件包来使用网络尚不可用的 firebase 服务。 Auth、RTD 和存储运行良好,我现在正在尝试使用 RemoteConfig 和消息传递(还不是睾丸),但我在实例化 RemoteConfig 时遇到问题。 查看 firebase: ^7.3.0 包代码,我看到一个文件本身基本上是一个单例,例如 App 和 RemoteConfig 部分是:

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);
}

所以我在自己的单例中将 Firebase 应用程序初始化为:

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;
    }
  }
}

然后为了访问 Auth、实时数据库和存储,它完美地实例化它并将其用作:

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

问题是在将 RemoteConfig 实例化为

RemoteConfig remoteConfig = FirebaseWeb.instance.remoteConfig;

在 Chrome 控制台中打印 FirebaseWeb get remoteConfig called 超过 7k 次..

我在初始化应用程序和远程配置时做错了什么? 非常感谢

已解决.. 除了 App() 在单例中不需要初始化任何其他东西.. 只需在 类 中初始化 firebase App firebase = FirebaseWeb.instance.app; 你需要使用 remoteConfig 然后它就可用马上使用.. 猜想这对消息传递同样有效..

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');
    }
  }