[英]How I can get the right data for specific user when I did auth flow (log in, log out) with Flutter Firestore?
Here is my stream to get data from firebase inside Flutter这是我的 stream 从 Flutter 里面的 firebase 获取数据
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.在主视图中,我打电话从 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?我检查了一下,这不是来自 firestore 的 SignOut 功能的问题,我认为这是反应式快照的问题,因为我为特定用户获得了正确的快照,但前提是我重新启动我的应用程序并尝试登录。所以有人可以帮助解决这个问题问题?
without seeing the AuthController difficult to tell.没有看到 AuthController 很难分辨。
Get.find<AuthController>().user.value?.uid?? ' ';
You can replace this with FirebaseAuth.instace.currentUser.uid
您可以将其替换为FirebaseAuth.instace.currentUser.uid
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.