在Flutter FutureBuilder中将FirebaseAuth.instance.currentUser转换为Future时出现问题

9gm1akwq  于 2022-11-30  发布在  Flutter
关注(0)|答案(3)|浏览(130)

我是flutter的新手,我上过这门课程,它使用了旧版本的FirebaseAuth,它将FirebaseAuth.instance.currentUser()作为future返回给FutureBuilder。现在有了新版本的FirebaseAuth.instance.currentUser是一个用户,而不是一个未来。我该如何修改代码使其工作呢?
下面是页面代码:

import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';

import '../chat/message_bubble.dart';

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

我很感激你的帮助。非常感谢!

xxslljrj

xxslljrj1#

我认为我不需要FutureBuilder并将其删除。以下是最终代码:

import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';

import '../chat/message_bubble.dart';

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

我希望这对其他正在寻找答案的人有帮助。

hs1ihplo

hs1ihplo2#

Future<void> data() async {
      return FirebaseAuth.instance.currentUser;
    }

    return FutureBuilder(
        future: data(),
        builder: (ctx, futureSnapShot) {
          if (futureSnapShot.connectionState == ConnectionState.waiting) 
          {
            return CircularProgressIndicator();
          }
          return ListView.builder(
            reverse: true,
            itemCount: chatDocs.length,
            itemBuilder: (ctx, index) => MessageBubble(
              chatDocs[index]['text'],
              chatDocs[index]['userId'] ==
                  FirebaseAuth.instance.currentUser.uid,
            ),
          );
        });
toe95027

toe950273#

"谢谢你们,我已经完全进步了"

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:fullchatside/widgets/chats/message_bubble.dart';
class Messages extends StatelessWidget {
  const Messages({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return StreamBuilder<QuerySnapshot>(
        stream: FirebaseFirestore.instance
            .collection('chat')
            .orderBy('birthoftext', descending: true)
            .snapshots(),
        builder: (BuildContext cxt, AsyncSnapshot<QuerySnapshot> chatSnapshot) {
          if (chatSnapshot.connectionState == ConnectionState.waiting) {
            return const Center(
              child: CircularProgressIndicator(
                color: Colors.deepPurpleAccent,
              ),
            );
          }
          final chatDocs = chatSnapshot.data?.docs;
          Future<User?> data() async {
            return FirebaseAuth.instance.currentUser;
          }

          return FutureBuilder(
              future: data(),
              builder: (ctx, futureSnapShot) {
                return ListView.builder(
                  reverse: true,
                  itemBuilder: ((cxt, index) => MessageBubble(
                        chatDocs![index]['text'],
                        chatDocs[index]['userId'] ==
                            FirebaseAuth.instance.currentUser!.uid,
                      )),

                  //  Text(chatDocs ![index]['text'])),
                  physics: const BouncingScrollPhysics(),
                  itemCount: chatDocs?.length,
                );
              });
        });
  }
}



`

相关问题