Home > Mobile >  using Flutter to check a specific field value of a user
using Flutter to check a specific field value of a user

Time:07-02

how can i easily check a specific field - value of a relative user who has already been saved on firestore . so for example on firebase it has been recorded that the user's 'role'(field) is that of a 'client'(value)

this is a code snippet i primarliry used to chek the user:

 void checkRole(String name, String uid) async {
await firestore.collection('users').doc(uid).get().then((role) {
  if (role.data()!["client"] == true) {
    print("true");
  } else {
    print("false");
  }
});

im trying to call checkRole() on the snippet below , except I got an error saying "2 positional argument(s) expected, but 0 found":

  InkWell(
            onTap: () => {authController.checkRole()},
            child: Padding(
              padding: const EdgeInsets.only(left: 13.0, top: 15.0),
              child: Row(
                children: const [
                  Icon(
                    Icons.person,
                  ),
                  SizedBox(width: 9),
                  Text(
                    "check role",
                    style: TextStyle(
                      fontSize: 16,
                    ),
                  ),
                ],
              ),
            ),
          ),

CodePudding user response:

This error is caused because your method "checkRole" expects 2 arguments name and uid. But when you are calling the method authController.checkRole(), you are not passing any argument.

Since the function checkRole doesn't use name you could remove that.

void checkRole(String uid) async { 
  await firestore.collection('users').doc(uid).get().then((role) {
  if (role.data()!["client"] == true) {
      print("true");
  } else {
      print("false");
  }
});

And

InkWell(
        onTap: () => {authController.checkRole('youruid')},
        child: Padding(
          padding: const EdgeInsets.only(left: 13.0, top: 15.0),
          child: Row(
            children: const [
              Icon(
                Icons.person,
              ),
              SizedBox(width: 9),
              Text(
                "check role",
                style: TextStyle(
                  fontSize: 16,
                ),
              ),
            ],
          ),
        ),
      ),

CodePudding user response:

You defined your check role function with two parameters. It should be passed while calling the function.

InkWell(
   onTap: () => authController.checkRole('your name', 'your uid'),
   child: Padding(
       padding: const EdgeInsets.only(left: 13.0, top: 15.0),
       child: Row(
           children: const [
              Icon(Icons.person),
              SizedBox(width: 9),
              Text(
                  "check role",
                  style: TextStyle(
                     fontSize: 16,
                  ),
                 ),
              ],
        ),
    ),
),

And in your check role function, there is no use of the name parameter. So you can remove it and call accordingly.

  • Related