0

I am trying to clean up my code by referencing a class that contains all my cloud functions, however, I am running into a mini bug that is driving me nuts.

Here is the class that holds my cloud logic::

class FirebaseCloudStorage {
  final job = FirebaseFirestore.instance.collection('job');
  final jobApplication =
      FirebaseFirestore.instance.collection('job application');
  final user = FirebaseFirestore.instance.collection('user');
  final groupUser = FirebaseFirestore.instance.collection('group users');
  final group = FirebaseFirestore.instance.collection('group');
  final currentUser = AuthService.firebase().currentUser!;
  final chat = FirebaseFirestore.instance.collection('chat');
  final chatMessage = FirebaseFirestore.instance.collection('chat messages');

// Chat messages
  getChatMessages({required chatMessageId}) {
    chatMessage
        .where(chatMessageIdColumn, isEqualTo: chatMessageId)
        .orderBy(
          chatMessageCreatedAtColumn,
          descending: true,
        )
        .snapshots();
  }


// singleton
  static final FirebaseCloudStorage _shared =
      FirebaseCloudStorage._sharedInstance();
  FirebaseCloudStorage._sharedInstance();
  factory FirebaseCloudStorage() => _shared;
}

Here is the code before implementing the class FirebaseCloudStorage, it works perfect:

class Messages extends StatelessWidget {
  final currentUser = AuthService.firebase().currentUser!;
  User? _currentUser = FirebaseAuth.instance.currentUser;
  String chatId;
  Messages({required this.chatId});
//   final _couldChatMessagesService = FirebaseCloudStorage();

  @override
  Widget build(BuildContext context) {
    return StreamBuilder(
      stream: FirebaseFirestore.instance
          .collection('chat messages')
          .where(chatMessageIdColumn, isEqualTo: chatId)
          .orderBy(
            chatMessageCreatedAtColumn,
            descending: true,
          )
          .snapshots(),
      builder: (ctx, chatSnapshot) {
        if (chatSnapshot.connectionState == ConnectionState.waiting) {
          return const Center(
            child: CircularProgressIndicator(),
          );
        }
        final chatDocs = chatSnapshot.data?.docs;

        return ListView.builder(
          reverse: true,
          itemCount: chatDocs?.length,
          itemBuilder: (ctx, index) => MessageBubble(
            message: chatDocs?[index][chatMessageTextColumn],
            isMe: chatDocs?[index][chatMessageUserIdColumn] == currentUser.id,
            key: ValueKey(chatDocs?[index].id),
            // userName: chatDocs?[index]['userId'],
          ),
        );
      },
    );
  }
}


here is my code after implementing the class FirebaseCloudStorage ::

class Messages extends StatelessWidget {
  final currentUser = AuthService.firebase().currentUser!;
  final User? _currentUser = FirebaseAuth.instance.currentUser;
  String chatId;
  // ignore: use_key_in_widget_constructors
  Messages({required this.chatId});
  final _couldChatMessagesService = FirebaseCloudStorage();

  @override
  Widget build(BuildContext context) {
    return StreamBuilder(
      stream: _couldChatMessagesService.getChatMessages(chatMessageId: chatId),
      builder: (ctx, chatSnapshot) {
        if (chatSnapshot.connectionState == ConnectionState.waiting) {
          return const Center(
            child: CircularProgressIndicator(),
          );
        }
        final chatDocs = chatSnapshot.data?.docs;    //ERROR RIGHT HERE,


        return ListView.builder(
          reverse: true,
          itemCount: chatDocs?.length,
          itemBuilder: (ctx, index) => MessageBubble(
            message: chatDocs?[index][chatMessageTextColumn],
            isMe: chatDocs?[index][chatMessageUserIdColumn] == currentUser.id,
            key: ValueKey(chatDocs?[index].id),
            // userName: chatDocs?[index]['userId'],
          ),
        );
      },
    );
  }
}


The error is the following: The getter 'docs' isn't defined for the type 'Object'. Try importing the library that defines 'docs', correcting the name to the name of an existing getter, or defining a getter or fields named 'docs'

Image of error

Juan Casas
  • 268
  • 2
  • 13

1 Answers1

0

I think I found the missing one, if you do this kind of structure it will work:

     stream: FirebaseFirestore.instance.collection("users").snapshots(),
              builder: (ctx, AsyncSnapshot<QuerySnapshot> chatSnapshot) {

final chatDocs = chatSnapshot.data?.docs;

I think you forgot this keyword: AsyncSnapshot<QuerySnapshot>

Nijat Namazzade
  • 652
  • 5
  • 15