在Flutter中如何在firebase中添加用户ID

li9yvcax  于 2023-02-16  发布在  Flutter
关注(0)|答案(2)|浏览(145)

如何把用户ID在这个代码时,我正在生成一个待办事项列表,我需要检索的任务,某个用户?

Future<void> create(String todo, String description) async {
    try {
      await firestore.collection("Todo").add({
        'todo': todo,
        'description': description,
        'timestamp': FieldValue.serverTimestamp()
      });
    } catch (e) {
      print(e);
    }
  }
uxh89sit

uxh89sit1#

我解决了我的问题

Future<void> create(String todo, String description) async {
    String? Uid= FirebaseAuth.instance.currentUser!.uid;
    print(Uid);
    print(FirebaseAuth.instance.currentUser!.uid);
    try {
      await firestore
          .collection("TodoList")
          .doc(Uid)
          .collection("Todo")
          .add({
        'todo': todo,
        'description': description,
        'timestamp': FieldValue.serverTimestamp()
      });
    } catch (e) {
      print(e);
    }
  }
kmbjn2e3

kmbjn2e32#

Future<void> signUpController(email, password, username, phone) async {
print("$email,$password,$username,$phone");
try {
  CommanDialog.showLoading();
  final credential =
      await FirebaseAuth.instance.createUserWithEmailAndPassword(
    email: email.trim(),
    password: password,
  );
  print(credential);
  CommanDialog.hideLoading();

  try {
    CommanDialog.showLoading();
    var response =
        await FirebaseFirestore.instance.collection('userlist').add({
      'user_id': credential.user!.uid,
      'user_name': username,
      'phone': phone,
      'password': password,
      'joindate': DateTime.now().millisecondsSinceEpoch,
      'email': email
    });
    print("response:: ${response.toString()}");
    CommanDialog.hideLoading();
    Get.back();
  } catch (execption) {
    CommanDialog.hideLoading();
    print("error saving data ${execption}");
  }

  Get.back();
} on FirebaseAuthException catch (e) {
  CommanDialog.hideLoading();
  if (e.code == 'weak-password') {
    CommanDialog.showErrorDialog(
        description: "The password provided is too weak.");
    print('The password provided is too weak.');
  } else if (e.code == 'email-already-in-use') {
    CommanDialog.showErrorDialog(
        description: "The account already exists for that email.");
    print('The account already exists for that email.');
  }
} catch (e) {
  CommanDialog.hideLoading();
  CommanDialog.showErrorDialog(description: "something went wrong");
  print(e);
}

}

相关问题