'Problem converting FirebaseAuth.instance.currentUser to Future in Flutter FutureBuilder
I am new to flutter and have taken this course which uses an older version of FirebaseAuth that returns FirebaseAuth.instance.currentUser() as future to FutureBuilder. Now with new version FirebaseAuth.instance.currentUser is a User not a Future. How can I change the code to make it work?
Here is the page code:
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,
),
);
},
);
},
);
}
}
I appreciate your help. Thanks a lot!
Solution 1:[1]
I concluded that I do not need the FutureBuilder and removed it. Here is the final code:
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,
),
);
},
);
}
}
I hope this helps others who are looking for an answer.
Solution 2:[2]
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,
),
);
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | John Smith |
Solution 2 | Praful Bhavsar |