2

I am trying to create a class with methods inside to a specific Authentication process

So I have created a file MyAuthMethods.dart

class UserAuth {

  static int seconds = 10000000;
  static String username;
  static String password;
  static String key = "abc123";

  static Future<String> _generateAuthCode(seconds, username, password, key) async{
    var result = "something";
    print("Seconds: seconds, Username: username, Password: password, Key: key");

    return result;
  }

}

on my form (FormScreen.dart), a button has onPressed to execute the function

onPressed:(){
  UserAuth._generateAuthCode(UserAuth.seconds, "username", "password", UserAuth.key);
}

but it does not work. It says:

error: The method '_generateAuthCode' isn't defined for the class 'UserAuth'.

What do I need to change?

2 Answers 2

4

There are no keywords like public, protected and private in Dart. In order to make a variable or function private to a class, the name of the variable or function needs to start with underscore(_). Variables/function without underscore(_) are public. You have defined a private function and accessing the private function. You can fix by making the function public: in order to do so just remove the underscore from the function, make it generateAuthCode.

class UserAuth {

  static int seconds = 10000000;
  static String username;
  static String password;
  static String key = "abc123";

  static Future<String> generateAuthCode(seconds, username, password, key) async{
    var result = "something";
    print("Seconds: seconds, Username: username, Password: password, Key: key");

    return result;
  }

}
2
  • Go it. Sorry but new to dart. Thanks a lot. Commented Oct 12, 2018 at 4:55
  • @RaffaeleColleo No problem. I also got stuck on this one too. Commented Oct 12, 2018 at 5:07
1

Unlike Java, Dart doesn’t have the keywords public, protected, and private. If an identifier starts with an underscore (_), it’s private to its library.

Libraries and visibility

So _generateAuthCode is the private method for your class , so that only you are not allowed to access.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.