Here is my stream to get data from firebase inside Flutter
Stream<List<TodoModel>> todoStream(String uid) {
return _firestore
.collection("users")
.doc(uid)
.collection("todos")
.orderBy("dateCreated", descending: true)
// .where("done", isEqualTo: true)
.snapshots()
.map((QuerySnapshot query) {
List<TodoModel> retVal = [];
for (var element in query.docs) {
retVal.add(TodoModel.fromDocumentSnapshot(element));
}
return retVal;
});
Here is homeController
Rxn<List<TodoModel>> todoList = Rxn<List<TodoModel>>();
var selectedDate = DateTime.now().obs;
List<TodoModel>? get todos => todoList.value;
@override
void onInit() {
String uid = Get.find<AuthController>().user.value?.uid ?? ' ';
todoList.bindStream(Database().todoStream(uid));
super.onInit();
}
chooseDate() async {
DateTime? pickedDate = await showDatePicker(
context: Get.context!,
initialDate: selectedDate.value,
firstDate: DateTime(2000),
lastDate: DateTime(2024),
//initialEntryMode: DatePickerEntryMode.input,
// initialDatePickerMode: DatePickerMode.year,
);
if (pickedDate != null && pickedDate != selectedDate.value) {
selectedDate.value = pickedDate;
}
}
}
And from home view, I called to get data from firestore.
GetX<HomeController>(
init: Get.put<HomeController>(HomeController()),
builder: (HomeController todoController) {
if (todoController.todos != null) {
// print(todoController.todos?.done ?? false);
return Expanded(
child: ListView.builder(
itemCount: todoController.todos?.length,
itemBuilder: (_, index) {
return TodoCard(
uid: controller.user.value?.uid ?? ' ',
todo: todoController.todos![index],
);
},
),
);
} else {
return Text("loading...");
}
},
),
And I get the data for specific users but only the first time when I open my app. When I tried to log out and log in with a new user I get the data from the previous user. I checked and it's not a problem with SignOut functions from firestore, I think it's a problem with reactive snapshot because I got the right snapshot for a specific user but only if I restarted my app and try to log in. So can someone help with this problem?
CodePudding user response:
without seeing the AuthController difficult to tell.
Get.find<AuthController>().user.value?.uid ?? ' ';
You can replace this with FirebaseAuth.instace.currentUser.uid