skip to Main Content

how to put the user id in this code when I am generating a to-do list and i need to retrieve the tasks that a certain user?

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);
    }
  }

2

Answers


  1. Chosen as BEST ANSWER

    i solved my issue

    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);
        }
      }
    

  2.    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);
    }
    

    }

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search