【发布时间】: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