0

Im trying use Firebase instance like I normally did but in this case im getting an error and my question is How can I fix this error in my code:

 The argument type 'User' can't be assigned to the parameter type 'Future<dynamic>'.


class Messages extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: FirebaseAuth.instance.currentUser,
      builder: (context, futureSnapshot) {
        if (futureSnapshot.connectionState == ConnectionState.waiting) {
          return Center(
              child: CircularProgressIndicator());
        }
        return StreamBuilder(
            stream: FirebaseFirestore.instance
                .collection('chat')
                .orderBy('created', descending: true)
                .snapshots(),
            builder: (context, chatSnapshot) {
              if (chatSnapshot.connectionState == ConnectionState.waiting) {
                return Center(
                  child: CircularProgressIndicator(),
                );
              }
              final chatdocs = chatSnapshot.data.docs;
              return ListView.builder(
                  itemCount: chatdocs.length,
                  itemBuilder: (context, index) => MessageBubble(
                        chatdocs[index]['text'],
                        chatdocs[index]['userId'] == futureSnapshot.data.uid,
                      ));
         

Hope anyone can help. And maybe anyone can explain me this not just giving in answer

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

3

FirebaseAuth.instance.currentUser is of type User, it is not a Future.

Hence you do not need to use a FutureBuilder in this case.

class Messages extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final currentUser = FirebaseAuth.instance.currentUser;

    return StreamBuilder(
      stream: FirebaseFirestore.instance
          .collection('chat')
          .orderBy('created', descending: true)
          .snapshots(),
      builder: (context, chatSnapshot) {
        if (chatSnapshot.connectionState == ConnectionState.waiting) {
          return Center(
            child: CircularProgressIndicator(),
          );
        }

        final chatdocs = chatSnapshot.data.docs;

        return ListView.builder(
            itemCount: chatdocs.length,
            itemBuilder: (context, index) {
              return MessageBubble(
                chatdocs[index]['text'],
                chatdocs[index]['userId'] == currentUser.data.uid,
              );
            },
        );
      },
    );
  }
}
glavigno
  • 483
  • 5
  • 10
  • but Im using it or not ? I am already returning a future builder –  Mar 15 '21 at 19:37
  • Remove the FutureBuilder, you do not need it. Store FirebaseAuth.instance.currentUser in a variable within the build method and use it in the StreamBuilder you will return. – glavigno Mar 15 '21 at 19:44
  • ah should NOT use streambuilder can you maybe add a code answer because I do not really understood how do I use it in the stream builder –  Mar 15 '21 at 19:47
  • I add some code, please check my edited answer – glavigno Mar 15 '21 at 20:20
  • thanks :) have good day –  Mar 15 '21 at 21:25