我是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,
),
);
},
);
},
);
}
}
我很感激你的帮助。非常感谢!
3条答案
按热度按时间xxslljrj1#
我认为我不需要FutureBuilder并将其删除。以下是最终代码:
我希望这对其他正在寻找答案的人有帮助。
hs1ihplo2#
toe950273#
"谢谢你们,我已经完全进步了"