I'm setting an User profile and I have a collection in my firestore which contain first 'users', in this I have the ID of this user and finally I can display name, uid etc. The problem is I want to print that name in my front end but I can't access this collection because I don't know how to use the current uid in my .doc(uid), (uid is undefined), I used a future method to get the uid but I don't know how to connect these. Hope you help me !
this is my frontend code
FutureBuilder(
future: FirebaseFirestore.instance
.collection('users')
.doc(uid)
.get(), //tryna to use that collection but uid is not defined correctly
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
var name = snapshot.data as DocumentSnapshot;
return Text(name['displayName'],
);
} else {
return Text("Loading...");
}
},
)
and my provider current User Id code
Future<String> inputData() async {
final User? user = _auth.currentUser;
final uid = user!.uid;
// here you write the codes to input the data into firestore
return uid;
}
I've updated frontend code here:
FutureBuilder(
future: FirebaseFirestore.instance
.collection('users')
.doc(FirebaseAuth.instance.currentUser!.uid)
.get(), //tryna to use that collection but uid is not defined correctly and I need a void but
// I have one in my auhtprovider
//solution ? create id from here : use my provider
builder: (context, snapshot) {
if (snapshot.hasData)
return Text("Loading...");
if (snapshot.data == null) {
print('Document does not exist on the database');
}else{
return Text("Researching data...");
}
if (snapshot.connectionState == ConnectionState.done) {
var name = snapshot.data as DocumentSnapshot;
return Text(name['displayName'],
);
} else {
return Text("Loading..");
}
},
)
and UID ?
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
Future userSetup(String displayName) async {
CollectionReference users = FirebaseFirestore.instance.collection('Users');
FirebaseAuth auth = FirebaseAuth.instance;
String uid = auth.currentUser!.uid.toString();
await users.doc(uid).set({'displayName': displayName, 'uid': uid });
final result = await users.doc(uid).get();
final data = result.data() as Map<String, dynamic>;
return data['displayName'];
}
Plus a photo of my firestore document :