Search code examples
fluttergoogle-cloud-firestorestreamfuturestream-builder

StreamBuilder is not showing data from firestore


I am using streambuilder to display snapshot data but it is not displaying. The screen is just blank but When I use the future builder with get() methode it display the data but I want realtime changes. I am new to flutter please help me with this. here is code.

class TalentScreen2 extends StatelessWidget {
  final Query _fetchFavUser = FirebaseRepo.instance.fetchFavUsers();

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        body: Column(
          children: [
            Text('Talent Screen 2(Favourites)'),
            Expanded(child: _retrieveData(context))
          ],
        ),
      ),
    );
  }

  Widget _retrieveData(BuildContext context) => StreamBuilder<QuerySnapshot>(
      stream: _fetchFavUser.snapshots(),
      builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
        if (snapshot.hasError) return const Text('Something went wrong');
        if (!snapshot.hasData) return const Text('Alas! No data found');
        if (snapshot.connectionState == ConnectionState.waiting)
          return Center(
              child: CircularProgressIndicator(
            strokeWidth: 2.0,
          ));
        if (snapshot.connectionState == ConnectionState.done)
          return theUserInfo(snapshot.data.docs);

        return Container();
      });

  Widget theUserInfo(List<QueryDocumentSnapshot> data) {
    return ListView.builder(
        shrinkWrap: true,
        itemCount: data.length,
        itemBuilder: (BuildContext context, int index) {
          var uid = data[index]['uid'];
          TalentHireFavModel userData = TalentHireFavModel.fromMap(
            data[index].data(),
          );

          return Card(
            child: Column(
              children: <Widget>[
                Text(data[index]['orderBy']),
                //  Text(userData.name ?? ''),
                Text(userData.categories),
                Text(userData.skills),
                //   Text(userData.country ?? ''),
                Text(userData.phoneNo),
                Text(userData.hourlyRate),
                Text(userData.professionalOverview),
                Text(userData.skills),
                Text(userData.expert),
                //   Text(userData.createdAt ?? ''),
                _iconButton(userData.uid, context),
              ],
            ),
          );
        });
  }

  Future<DocumentSnapshot> fetch(data) async =>
      await FirebaseRepo.instance.fetchWorkerUserData(data);

  Widget _iconButton(uid, context) {
    return IconButton(
        icon: Icon(Icons.favorite),
        onPressed: () {
          BlocProvider.of<TalentFavCubit>(context).removeTalentFav(uid);
        });
  }
}

and here is the firestore query methode where I am just applying simple query to fetch all documents and display them. I want real-time changes

Query fetchFavUsers() {
    var data = _firestore
        .collection('workerField')
        .doc(getCurrentUser().uid)
        .collection('favourites')
        //   .where('uid', isNotEqualTo: getCurrentUser().uid)
        .orderBy('orderBy', descending: true);
    return data;
  }

Solution

  • The solution is to just return the function. Get that method out of if statement and place it in just return statement.