【问题标题】:Accessing RemoteConfig create loop Flutter Web访问 RemoteConfig 创建循环 Flutter Web
【发布时间】:2020-10-28 02:22:00
【问题描述】:

我正在使用 firebase: ^7.3.0 包来使用尚未可用于网络的 firebase 服务。 Auth、RTD 和 Storage 运行良好,我现在正在尝试使用 RemoteConfig 和 Messaging(还不是 testes),但我在实例化 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 次..

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

【问题讨论】:

    标签: firebase firebase-cloud-messaging flutter-web firebase-remote-config


    【解决方案1】:

    已解决.. 除了 App() 在单例中不需要初始化其他任何东西。只需在需要使用 remoteConfig 的类中初始化 firebase App firebase = FirebaseWeb.instance.app; ,然后就可以立即使用它了。猜猜看同样适用于消息传递..

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-04
      • 2023-03-11
      • 1970-01-01
      • 2023-03-26
      • 2014-05-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多