0

I want to create a streambuilder to download multiple user-profiles from firebase. But to know which users are needed I have to get the user-ids at first, these are stored in an array. So I created a method to download the array and after this is done the streambuilder loads the user-data for each user-id from the array. Here's the code:

Method to get the array (executed in initState()):

Stream<QuerySnapshot> stream() async* {
job = Job.fromJson(await FirebaseFirestore.instance
    .collection("jobs")
    .doc(widget.jobId)
    .get());
applicants = job.applicants;
await FirebaseFirestore.instance
    .collection('users')
    .where('uid', whereIn: applicants)
    .snapshots();
}

And the streambuilder in the scaffolds' body:

body: isLoading
      ? Center(child: Container(child: CircularProgressIndicator()))
      : applicants.isEmpty
          ? Center(
              child: Text("no values"),
            )
          : StreamBuilder<QuerySnapshot>(
              stream: stream(),
              builder: (context, snapshot) {
                if (!snapshot.hasData) {
                  return Center(child: CircularProgressIndicator());
                } else { xy }

So my question is if there's a possibility to combine the first method and the stream. Because at the moment the user can't get any update if an application is withdrawn while using the screen.

beax07
  • 11
  • 3
  • check https://dart.dev/articles/libraries/creating-streams – pskink Apr 16 '22 at 07:13
  • thanks a lot, I have not heard about async* for streams before. Great hint! – beax07 Apr 16 '22 at 07:16
  • While using async* it seems to be a problem with returning the snapshots from the function, because of the rules for async* methods. But now, the snapshot.hasData return null all the time and the data can't be displayed. Do you know a way to avoid this? – beax07 Apr 16 '22 at 11:13
  • I also updated the code above. – beax07 Apr 16 '22 at 11:16
  • this is because you are not calling any `yield` statements (stream is empty), like `yield* your_final_firebase_stream;` so instead of second `await` use `yield*` – pskink Apr 16 '22 at 11:56

0 Answers0