我正在做一个Flutter项目,当我试图连接到Firestore使用一个次要的基础应用程序。
当我尝试连接到数据库时,出现以下错误
secAppFirestore
.collection("yos")
:
“未处理的异常:[core/no-app]未创建Firebase应用程序'[DEFAULT]'-调用Firebase.initializeApp”
我的代码
void main() async {
// Avoid errors caused by flutter upgrade.
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
name: "yostest2-xxxxx",
options: FirebaseOptions(
apiKey: "AIzaSy...",
appId: "1:68080...",
messagingSenderId: "",
projectId: "yostest2-xxxxx",
storageBucket: "yostest2-xxxxx.appspot.com",
databaseURL: 'https://yostest2-xxxxx.firebaseio.com',
),
);
runApp(const MyApp());
}
....
void _incrementCounter() async {
FirebaseApp secondaryApp = Firebase.app('yostest2-xxxxx');
FirebaseFirestore secAppFirestore =
FirebaseFirestore.instanceFor(app: secondaryApp);
await secAppFirestore
.collection("yos")
.add({"timestamp": FieldValue.serverTimestamp()});
setState(() {
_counter++;
});
}
2条答案
按热度按时间s3fp2yjn1#
您需要先调用
Firebase.initializeApp();
来初始化默认应用程序,然后再为辅助应用程序调用Firebase.initializeApp(name: "yostest2-xxxxx",...)
。请将其放在当前init行的上方并等待它。如果您在热重新加载时遇到问题,那么您可能还需要跟踪是否已经通过迭代Firebase.apps[]调用了initializeApp。
rhfm7lfc2#
但是,如果您希望将Firestore与辅助Firebase应用程序一起使用,请使用instanceFor方法:
FirebaseApp辅助应用程序= Firebase.app(“辅助应用程序”); FirebaseFirestore = FirebaseFirestore.示例对于(应用程序:辅助应用程序);
有关详细信息,请查看以下链接:https://firebase.flutter.dev/docs/firestore/usage/