Flutter:一个应用程序中有多个Firebase项目,但显示的数据不正确

时间:2019-06-10 20:55:34

标签: firebase flutter firebase-authentication google-cloud-firestore

最近几天,我花了大量时间阅读一些SO问题和教程。我想要实现的是,我的flutter应用程序的用户可以选择一个Firebase项目并使用电子邮件/密码登录。登录后,显然,应该显示相应数据库的正确数据。那就是我失败的地方。

一段时间后,阅读了SO的一些站点和问题,然后我转到了以下站点以获取登录名的第一部分。

https://firebase.googleblog.com/2016/12/working-with-multiple-firebase-projects-in-an-android-app.html

阅读完本文后,我能够成功登录到定义的firebase项目。

我怎么知道登录成功?我将项目中的用户uid与控制台中我的应用程序中的print语句进行了比较。那就是证明我对非默认项目的配置是正确的。

但是现在我无法解决的主要问题。 登录后,数据始终来自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(),
      ),
    );
  }

提供的Auth类是一种连接到Firebase SDK的服务,还可以配置非默认应用程序以创建所需的Firebase身份验证

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

登录后,应用程序会将用户重定向到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(),
                  );
              }
            },
          ),
        ),
      ),

一开始,我只有一个项目要连接,并且数据正确。但是,现在我已使用正确的用户uid成功连接到另一个项目,但是数据始终来自google-service.json定义的默认项目。 在这一点上,我不知道为什么会这样。

有人有建议或想法吗?

1 个答案:

答案 0 :(得分:1)

您基于` ClientConfig config = new ClientConfig(); HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("admin", "admin123"); Client client = ClientBuilder.newClient(config); client.register(feature); WebTarget webTarget = client.target(url); Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON); Response response = invocationBuilder.get(); int respCode = response.getStatus() ; String policyResponse = response.readEntity(String.class) ; System.out.println(policyResponse); System.out.println(response); System.out.println(respCode); //Reading the policy search response return policyResponse; } catch (Exception e) { System.out.println(e.getLocalizedMessage()); e.printStackTrace(); return e.toString(); } 创建_stream,这将为您提供默认的firebase应用,如文档所述:

Firestore.instance

因此,您总是从默认项目中获取数据。 要解决此问题,您需要使用/// Gets the instance of Firestore for the default Firebase app. static Firestore get instance => Firestore(); 创建的应用创建您的Firestore。

所以替换:

FirebaseApp.configure()

使用

_stream = Firestore.instance.collection(collection).snapshots();
相关问题