Dart 创建自己的类
我试图创建一个类,其中包含特定身份验证过程的方法 因此,我创建了一个文件MyAuthMethods.dartDart 创建自己的类,dart,flutter,Dart,Flutter,我试图创建一个类,其中包含特定身份验证过程的方法 因此,我创建了一个文件MyAuthMethods.dart class UserAuth { static int seconds = 10000000; static String username; static String password; static String key = "abc123"; static Future<String> _generateAuthCode(seconds, us
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;
}
}
但它不起作用。它说:
error: The method '_generateAuthCode' isn't defined for the class 'UserAuth'.
我需要更改什么?与Java不同,Dart没有public、protected和private等关键字。如果标识符以下划线(\)开头,则它对其库是私有的
因此_generateAuthCode是类的私有方法,因此只有您不允许访问。Dart中没有像public、protected和private这样的关键字。为了使变量或函数成为类的私有变量或函数,变量或函数的名称需要以下划线(
\uu
)开头。不带下划线(
)的变量/函数是公共的。您已经定义了一个私有函数并访问了该私有函数。您可以通过公开函数进行修复:为此,只需从函数中删除下划线,使其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;
}
}
类UserAuth{
静态整数秒=10000000;
静态字符串用户名;
静态字符串密码;
静态字符串键=“abc123”;
静态未来生成代码(秒、用户名、密码、密钥)异步{
var result=“某物”;
打印(“秒:秒,用户名:用户名,密码:密码,钥匙:钥匙”);
返回结果;
}
}
开始吧。对不起,我是达特的新手。非常感谢。@RaffaeleColleo没问题。我也被困在这个问题上了。
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;
}
}