Search code examples
firebaseflutterdartasync-awaitstream-builder

my Stream is not updating when isTeacher() function is called


what I am trying to achieve is load data from a specific collection(teacher) of my database. So I am using a function called isTeacher(). which checks if the current user's uid belongs in that collection. if not then it is a student. it stores the value in a string called value. so when I am using stream builder to load data available in their specific collection or documents, my stream builder shows circular progress and after that, it doesn't load the data. Any help is appreciated. Thank you

`class MyClasses extends StatefulWidget {
  @override
  _MyClasses createState() => _MyClasses();
}

String value;
String classPassword;
List<dynamic> catchUserDetails = [];

class _MyClasses extends State<MyClasses> { 

Future isTeacher() {
    return FirebaseFirestore.instance
        .collection('teacher')
        .doc(FirebaseAuth.instance.currentUser.uid)
        .get()
        .then((DocumentSnapshot doc) {
      value = doc.exists.toString();
      print(doc.data());
      print(value);
      print('isteacher called in method');
    });
  }


@override
  Widget build(BuildContext context) {
    isTeacher();
    return Scaffold(
    body: SafeArea(
        child: StreamBuilder(
          stream: FirebaseFirestore.instance
              .collection(value)
              .doc(FirebaseAuth.instance.currentUser.uid)
              .collection('class')
              .snapshots(),
          builder: (context, snapshot) {
            if (!snapshot.hasData) {
              return Center(
                child: CircularProgressIndicator(
                  backgroundColor: Colors.lightBlueAccent,
                ),
              );
            } else {
              final messages = snapshot.data.documents.reversed;

              List<GenerateClass> messageBubbles = [];
              for (var message in messages) {
                final messageText = message.data()['className'];
                final messageBubble = GenerateClass(
                  classID: messageText,
                  //nnouncementID: i,
                );
                messageBubbles.add(messageBubble);
              }

              return ListView(
                //itemExtent: 100,
                children: messageBubbles,
              );
            }
          },
        ),
      ),
    );`

Solution

  • Solved it by using a FutureBuilder

    FutureBuilder(
            future: isTeacher(),
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                return StreamBuilder();