Phi*_*ahn 7 firebase firebase-authentication flutter google-cloud-firestore
最近几天我花了很多时间阅读了几个 SO 问题和教程。我想要实现的是,我的 flutter 应用程序的用户可以选择一个 firebase 项目并使用电子邮件/密码登录。登录后,显然应该显示相应数据库的正确数据。这就是我失败的地方。
在阅读了一些网站和来自 SO 的问题一段时间后,我使用以下网站获取登录的第一部分。
完成本文后,我能够成功登录到我定义的 firebase 项目。
我怎么知道登录成功了?我将项目中的用户 uid 与控制台中我的应用程序中的打印语句进行了比较。这证明了我对非默认项目的配置是正确的。
但现在我无法解决的主要问题。登录后,数据始终来自 google-service.json 的默认 firebase 项目。
对于状态管理,我选择提供程序包,正如他们在 I/O '19 中提到的那样。所以在我的 main.dart 中,我用 MultipleProvider 包装了整个应用程序:
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider<LoginModel>(
builder: (_) => LoginModel(),
),
ChangeNotifierProvider<Auth>(
builder: (_) => Auth(),
),
],
child: MaterialApp(
title: 'Breaking News Tool',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: RootPage(),
),
);
}
Run Code Online (Sandbox Code Playgroud)
提供的 Auth 类是一个服务,它连接到 firebase sdk 并配置非默认应用程序来创建所需的 firebase auth
abstract class BaseAuth {
getDefaultAuth();
getAbnAuth();
...
}
class Auth with ChangeNotifier implements BaseAuth {
...
Auth() {
_configureAbnApp();
_configureProdApp();
}
getDefaultAuth() {
_firebaseAuth = FirebaseAuth.instance;
}
getAbnAuth() {
_firebaseAuth = FirebaseAuth.fromApp(_abnApp);
}
_configureAbnApp() {
FirebaseOptions abnOptions = FirebaseOptions(
databaseURL: 'https://[project-id].firebaseio.com',
apiKey: 'AIzaSxxxxxxxxxxxxxxxx,
googleAppID: '1:10591xxxxxxxxxxxxxxxxxxx');
FirebaseApp.configure(name: 'abn_database', options: abnOptions)
.then((result) {
_abnApp = result;
});
}
...
}
Run Code Online (Sandbox Code Playgroud)
登录后,应用程序会将用户重定向到 home_page (StatefulWidget)。这里我使用数据库的快照来显示数据。
_stream = Firestore.instance.collection(collection).snapshots();
...
Center(
child: Container(
padding: const EdgeInsets.all(10.0),
child: StreamBuilder<QuerySnapshot>(
stream: _stream,
builder:
(BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError)
return Text('Error: ${snapshot.error}');
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return Text('Loading...');
default:
return ListView(
children: snapshot.data.documents
.map((DocumentSnapshot document) {
return CustomCard(
docID: document.documentID,
title: document[title],
message: document[message],
fromDate: document[fromDate],
endDate: document[endDate],
disableApp: document[disableApp],
);
}).toList(),
);
}
},
),
),
),
Run Code Online (Sandbox Code Playgroud)
一开始,我只有一个项目要连接,而且数据是正确的。但是现在我使用正确的用户 uid 成功连接到另一个项目,但数据始终来自 google-service.json 定义的默认项目。在这一点上,我不知道为什么会发生这种情况。
有没有人有建议或想法?
您创建_stream基于Firestore.instance,这将为您提供默认的 firebase 应用程序,如文档中所述:
/// Gets the instance of Firestore for the default Firebase app.
static Firestore get instance => Firestore();
Run Code Online (Sandbox Code Playgroud)
因此,您始终从默认项目获取数据。要解决此问题,您需要使用创建的应用程序创建 Firestore FirebaseApp.configure()。
所以替换:
_stream = Firestore.instance.collection(collection).snapshots();
Run Code Online (Sandbox Code Playgroud)
和
_stream = Firestore(app: _abnApp).collection(collection).snapshots();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1888 次 |
| 最近记录: |