'why my streamBuilder get stuck in the active Connection State even though i got all the data in my App?

Im getting data in my Flutter Web App from my Firebase Collection but my stream builder connection state stuck in the active state and not proceeding to the done state

  static Stream<QuerySnapshot> getData({required String collectionName}) {
    return firebaseFirestore.collection(collectionName).snapshots();
  }

this is how im calling the stream

  late final Stream<QuerySnapshot> myStream;
  @override
  void initState() {
    super.initState();
    myStream = FireStoreServices.getData(collectionName: selectedRadio!);
  }

my build method

  @override
  Widget build(BuildContext context) {
    return StreamBuilder<QuerySnapshot>(
        stream: myStream,
        builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
          switch (snapshot.connectionState) {
            case ConnectionState.none:
              return const Center(
                child: Text('No Data'),
              );

            case ConnectionState.waiting:
              return const Center(
                child: CircularProgressIndicator(),
              );
              // **stream stuck here** as long as the application is running even though i **got all the data**
            case ConnectionState.active:
              return const Center(
                child: Text('Connection is active'),
              );

            case ConnectionState.done:
              return const Center(
                child: Text('Connection is DOne'),
              );
            default:
              return const Center(
                child: Text(' default state '),
              );
          }

Any Help Would be Appreciated



Solution 1:[1]

It's normal when you use StreamBuilder unlike FutureBuilder. Actually you should display data while the state is active or if the state is done.

Bellow a small example on how to use StreamBuilder:

  if (snapshot.connectionState == ConnectionState.waiting) {
    return CircularProgressIndicator();
  } else if (snapshot.connectionState == ConnectionState.active
      || snapshot.connectionState == ConnectionState.done) {
    if (snapshot.hasError) {
      return const Text('Error');
    } else if (snapshot.hasData) {
      return Text(
        snapshot.data.toString(),
        style: const TextStyle(color: Colors.teal, fontSize: 36)
      );
    } else {
      return const Text('Empty data');
    }
  } else {
    return Text('State: ${snapshot.connectionState}');
  }

You could find more info about StreamBuilder in this blog.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ExtraHassan