我得到了错误:Instance member 'updateUser' can't be accessed using static access.
,但我无法将updateUser方法转换为静态方法,因为它使用的是TextControllers。我该如何解决这个问题?
错误:Instance member 'updateUser' can't be accessed using static access.
使用未来updateUser:
Padding(
padding: const EdgeInsets.all(
8.0),
child: ElevatedButton(
child: Text("Bearbeiten"),
onPressed: () async {
if (_formKey.currentState.validate()) {
await AuthProvider.updateUser('1xorAjA7hRZfOdN3zpkAWI7spgp1',_birthDateInString,genderSelected, roleSelected);
_formKey.currentState.save();
Navigator.of(context).pop();
}
},
),
)
代码未来updateUser:
Future <void> updateUser(String id,String birthday, String gender, String role, )async{
return await FirebaseFirestore.instance
.collection('admins')
.doc(id)
.update({'username': usernameController, 'email': emailController, 'first name': firstNameController, 'last name': lastNameController, 'birthday': birthday, 'gender': gender, 'role': role,})
.then((value) => print("User Updated"))
.catchError((error) => print("Failed to update user: $error"));
}
发布于 2022-06-07 06:37:25
您正在尝试访问类内的方法,而不为类创建实例。因此,您需要将函数创建为静态方法,或者您可以创建类的实例并访问它。
将方法更改为静态的。
class AuthProvider {
static Future <void> updateUser(String id,String birthday, String gender, String role, )async{
return await Firebase......
}
}
或者创建类的实例。
if (_formKey.currentState.validate()) {
AuthProvider newObj = AuthProvider();
await newObj.updateUser('','','','');
// or simply await AuthProvider().updateUser('','','','');
}
如果仅限于上述方法,请尝试为类创建构造函数并像第二个方法一样访问它。
类:
class AuthProvider {
AuthProvier();
static Future <void> updateUser(String id,String birthday, String gender, String role, )async{
return await Firebase......
}
}
活动:
if (_formKey.currentState.validate()) {
AuthProvider newObj = AuthProvider();
await newObj.updateUser('','','','');
// or simply await AuthProvider().updateUser('','','','');
}
https://stackoverflow.com/questions/72518104
复制相似问题