简体   繁体   English

Flutter firebase,使用一个文档中的引用调用另一个文档

[英]Flutter firebase, use a reference in a document to call another document

I am using MultiProvider Stream with a database service with the firebase calls and a data model and using to and fromJson.我正在使用 MultiProvider Stream 和数据库服务以及 firebase 调用和数据 model 并使用 to 和 fromJson。

I am calling the Firestore User in my main file, and have a wrapper to call the user's document which holds the reference to the company they belong to, once the user data is retrieved we then call the company document in a userType screen, I then use the reference and pass it to the getter of the company document, but the document is being called before the document ref is passed.我在我的主文件中调用 Firestore 用户,并有一个包装器来调用用户的文档,其中包含对他们所属公司的引用,一旦检索到用户数据,我们然后在 userType 屏幕中调用公司文档,然后我使用引用并将其传递给公司文档的 getter,但在传递文档 ref 之前正在调用该文档。

I have tried to change the Stream to a Future but then I get an error on Provider.我试图将 Stream 更改为 Future,但随后在 Provider 上出现错误。

Database Service数据库服务

 Stream<Companies> streamCompanies() {
_userRef.get().then((DocumentSnapshot documentSnapshot) {
  if (documentSnapshot.exists) {
    print('HERE IS THE SNAPSHOT');
    print(documentSnapshot.get('companyID'));
    thisUserCompany = documentSnapshot.get('companyID');
  }
});
debugPrint('Database Service => GETTING COMPANY STREAM');
debugPrint(thisUserCompany);
return _database
    .collection('companies')
    .doc(thisUserCompany)
    .snapshots()
    .map(
      (snapshot) =>
          Companies.fromJson(snapshot.data() as Map<String, dynamic>),
    );

} }

Comapnies Class公司 Class

class Companies {
  final String uid;
  final String companyName;
  final String logoForPDF;

  Companies ({required this.uid, required this.companyName,
  required this.logoForPDF});

  Companies.fromJson(Map<String, dynamic> json)
      : uid = json['uid'],
        companyName = json['companyName'],
        logoForPDF = json['logoForPDF'];

  Map<String, dynamic> toJson() => {
        'uid': uid,
        'companyName': companyName,
        'logoForPDF': logoForPDF,
      };
      factory Companies.initialData() {
    return Companies(
      uid: 'Loading',
      companyName: 'Loading',
      logoForPDF: 'Loading',
    );
  }
}

MultiProvider多提供者

class HomeScreen extends StatelessWidget {
  const HomeScreen({super.key});

  @override
  Widget build(BuildContext context) {
    final user = Provider.of<UserData?>(context);
    return user != null
        ? MultiProvider(providers: [
            StreamProvider<Companies?>.value(
                initialData: Companies.initialData(),
                value: user != null
                    ? DatabaseService(uid: user.uid, companyID: user.companyID)
                        .streamCompanies()
                    : null),
          ], child: const ProfileSelector())
        : const CircularProgressIndicator();
  }
}

Ok so a little more perseverance I now have this working but I do not know if this is the correct way of doing this.好吧,所以再坚持一点,我现在有了这个工作,但我不知道这是否是正确的方法。

In my database file, I have changed the code to the following在我的数据库文件中,我已将代码更改为以下

  Stream<Companies> streamCompanies() async* {
    await _userRef.get().then((DocumentSnapshot documentSnapshot) {
      if (documentSnapshot.exists) {
        thisUserCompany = documentSnapshot.get('companyID');
      }
    });
     yield* _database
            .collection('companies')
            .doc(thisUserCompany)
            .snapshots()
            .map((snapshot) =>
                  Companies.fromJson(snapshot.data() as Map<String, dynamic>),
                );
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM