Reputation: 859
I am new to Firestore. I am stuck at getting the document ID from Firestore. Here I create the user with name and age parameters. When I click Submit, it submits to Cloud Firestore. Everything is Ok up to now.
class _MyHomePageState extends State<MyHomePage> {
late final TextEditingController name = TextEditingController();
late final TextEditingController age = TextEditingController();
final DBHelper _helper = DBHelper();
late final User _user=User();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextField(
controller: name,
decoration: const InputDecoration(hintText: "Name"),
),
TextField(
controller: age,
decoration: const InputDecoration(hintText: "Age"),
),
ElevatedButton(onPressed:(){
_user.setAge(age.text);
_user.setName(name.text);
print("Name: ${_user.name}");
_helper.addUser(_user);
Navigator.push(context, MaterialPageRoute(builder: (builder)=>MainPage( ,))); //here i need to push the document id
}, child: const Text("Submit"))
],
),
),
);
}
This is my custom User model
class User{
late final String userId;
final String name;
final String age;
User({required this.name, required this.age});
}
This is my service class for Firestore methods
class DBHelper{
// final User user = User();
final CollectionReference _reference= FirebaseFirestore.instance.collection("users"); // database path
//Adding user to Firestore
Future<void> addUser(User user){
return _reference.add({
'name':user.name,
'age':user.age,
}).then((value) => print("User added"))
.catchError((onError)=> print("Failed to add the user: $onError"));
}
}
Here is what I want: When I click on the submit button in the previous page. It will navigate to the following page. In this page, I want to show the "name" and "age" information. The only thing that is missing is the documentId
return FutureBuilder<DocumentSnapshot>(
future: _ref.doc(documentId).get(), //which document doesn't know
builder:
(BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (snapshot.hasError) {
return const Center(
child: Text(
"Bir hata oluştu.",
textScaleFactor: 3,
));
} else if (snapshot.hasData && !snapshot.data!.exists) {
return const Center(
child: Text(
"Döküman yok",
textScaleFactor: 3,
));
}
if (snapshot.connectionState == ConnectionState.done) {
Map<String, dynamic> data =
snapshot.data!.data() as Map<String, dynamic>;
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"Name: ${data['name']}",
textScaleFactor: 3,
),
Text(
"Age:${data['age']}",
textScaleFactor: 3,
),
],
)),
);
} else {
return const Center(child: CircularProgressIndicator());
}
});
Upvotes: 0
Views: 1108
Reputation: 1050
you can get the id on the metodh you used to submit infos
instead of return void,return a String(the id) then go to the next screen like that:
Future<String> addUser(User user)async {
return await _reference.add({
'name':user.name,
'age':user.age,
}).then((value) => var documentId=value.id)
and the button function should be:
ElevatedButton(onPressed:(){
_user.setAge(age.text);
_user.setName(name.text);
print("Name: ${_user.name}");
var id=await _helper.addUser(_user);
Navigator.push(Get.context!, MaterialPageRoute(builder:(builder)=>MainPage(documentI: id)));
}, child: const Text("Submit"))
],
),
Upvotes: 1