当我使用 Flutter Firestore 进行身份验证流程(登录、注销)时,如何为特定用户获取正确的数据?
How I can get the right data for specific user when I did auth flow (log in, log out) with Flutter Firestore?
这是我在 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;
});
这里是 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;
}
}
}
并且从主页视图,我调用从 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...");
}
},
),
而且我获得了特定用户的数据,但只是在我第一次打开我的应用程序时。当我尝试注销并使用新用户登录时,我从以前的用户那里获取数据。我检查了一下,这不是来自 firestore 的 SignOut 功能的问题,我认为这是反应式快照的问题,因为我为特定用户获得了正确的快照,但前提是我重新启动我的应用程序并尝试登录。所以有人可以帮助解决这个问题有问题吗?
没有看到AuthController很难分辨。
Get.find<AuthController>().user.value?.uid ?? ' ';
您可以将其替换为 FirebaseAuth.instace.currentUser.uid
这是我在 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;
});
这里是 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;
}
}
}
并且从主页视图,我调用从 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...");
}
},
),
而且我获得了特定用户的数据,但只是在我第一次打开我的应用程序时。当我尝试注销并使用新用户登录时,我从以前的用户那里获取数据。我检查了一下,这不是来自 firestore 的 SignOut 功能的问题,我认为这是反应式快照的问题,因为我为特定用户获得了正确的快照,但前提是我重新启动我的应用程序并尝试登录。所以有人可以帮助解决这个问题有问题吗?
没有看到AuthController很难分辨。
Get.find<AuthController>().user.value?.uid ?? ' ';
您可以将其替换为 FirebaseAuth.instace.currentUser.uid