简体   繁体   English

当我使用 Flutter Firestore 进行身份验证流程(登录、注销)时,如何为特定用户获取正确的数据?

[英]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.

相关问题 为什么使用 Firebase Auth 退出时会出现错误? - Why do I get an error when I log out with Firebase Auth? 有没有办法使用 firebase auth go sdk 注销特定用户? - Is there a way to log out a specific user using firebase auth go sdk? 如何使用 typescript 或 javascript 从 firestore 中的文档获取字段值并放入 console.log()? - How can I get a field value from a document in firestore with typescript or javascript and put in a console.log()? Flutter & Firestore:当Firestore无法获取特定字段时,如何安全获取数据? - Flutter & Firestore: What way to get data with safety when the Firestore can't get a specific field? 当我删除他在 firebase 上的帐户时,如何从应用程序中注销用户? - How to log out user from app when I delete his account on firebase? 如何使用 Flutter 从 Firestore 获取当前用户文档 ID/名称? - How can I get current user document id/name from Firestore with Flutter? Firestore 如何在 Flutter 中获取具有特定用户 ID 的特定数据 - Firestore how to fetch specific data with specific user id in Flutter flutter firestore 我如何过滤数据 - flutter firestore how I filter data 如何通过特定用户获取 firestore 中的数据 - how to get data in firestore by specific user 在 cloudwatch 中创建日志组时,如何触发 lambda? - How can I trigger a lambda when a log group is created in cloudwatch?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM