gpt4 book ai didi

firebase - 如何访问Future方法中存在的回调函数的返回值。 Dart

转载 作者:行者123 更新时间:2023-12-03 04:23:41 29 4
gpt4 key购买 nike

auth.dart

class AuthService {

final FirebaseAuth _auth = FirebaseAuth.instance;

// Sign in with Phone Number Future<bool>
Future signInWithPhone(String phone, BuildContext context) async {

final PhoneVerificationCompleted verificationCompleted = (AuthCredential credential) async {
..
};

final PhoneVerificationFailed verificationFailed = (AuthException exception) {
// This is the return value which i want to access outside of this callback
return exception.message;
};

final PhoneCodeSent codeSent = (String verificationId, [int forceResendingToken]) {
...
};

final PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout = (String verificationId) {
Navigator.of(context).pop();
...
};

await _auth.verifyPhoneNumber(
phoneNumber: phone,
timeout: Duration(seconds: 10),
verificationCompleted: verificationCompleted,
verificationFailed: verificationFailed,
codeSent: codeSent,
codeAutoRetrievalTimeout: codeAutoRetrievalTimeout
);

// Trying to access the callback value
print(verificationFailed);
}
}

login.dart
class LoginPage extends StatefulWidget {
@override
_LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {

@override
Widget build(BuildContext context) {
return Container(

// here i am calling the phonesign in method
await _auth.signInWithPhone(phoneNumber, context);

// and here i want to show the message

);
}

}

有什么问题?

问题是我想访问 verifyPhoneNumber 的任何回调的值,例如 verifyFailed ,它返回异常消息,并且我想在外部访问它,以便可以在其他小部件中使用它。

我到目前为止尝试了什么?

1)我尝试打印它,它返回 AuthException闭包:()=>字符串我不知道如何访问该值。

2)我尝试使用 setState ,但是由于我的类(class)不在 statefulWidget 内,所以我无法将该值分配给变量

*我为什么需要它? *

看我有两个页面,一个叫做 login.dart,第二个叫做 auth.dart。在我的身份验证页面中,我主要放置与后端相关的类和代码(以正确组织代码),而在登录页面中,我有一个有状态的小部件,电话文本字段并提交后,我会调用 signInWithPhone 方法。一切正常,但是如果出现任何错误(例如用户电话号码格式错误),则会触发 VerificationFailed 回调,因此我想在外部访问返回值(bcz我们无法直接获取返回值),这样我就可以从调用它的地方显示它。

谁能帮助我,以便我继续我的代码。

最佳答案

现在很清楚您要做什么。使用提供程序是一种方法。另一种方法是将回调传递给登录页面:

class LoginPage extends StatefulWidget {
final Function(String) verificationMessage;

const LoginPage({Key key, this.verificationMessage}) : super(key: key);

@override
_LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {


final PhoneVerificationFailed verificationFailed = (AuthException exception) {

widget.verificationMessage(exception.message);’
};
}

现在,只要您调用LoginPage,就只需传递一个回调函数
LoginPage(verificationMessage: (String message) => {
setState(() {
// Do what you want with the message
})
})

在您的情况下:

验证码
class AuthService {

**final Function(String) verificationMessage;**
**AuthService({@required this.verificationMessage});**

final FirebaseAuth _auth = FirebaseAuth.instance;

// Sign in with Phone Number Future<bool>
Future signInWithPhone(String phone, BuildContext context) async {

final PhoneVerificationCompleted verificationCompleted = (AuthCredential credential) async {
..
};

final PhoneVerificationFailed verificationFailed = (AuthException exception) {
// This is the return value which i want to access outside of this callback
**verificationMessage(exception.message);**
};

final PhoneCodeSent codeSent = (String verificationId, [int forceResendingToken]) {
...
};

final PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout = (String verificationId) {
Navigator.of(context).pop();
...
};

await _auth.verifyPhoneNumber(
phoneNumber: phone,
timeout: Duration(seconds: 10),
verificationCompleted: verificationCompleted,
verificationFailed: verificationFailed,
codeSent: codeSent,
codeAutoRetrievalTimeout: codeAutoRetrievalTimeout
);

// Trying to access the callback value
print(verificationFailed);
}
}

login.dart
class LoginPage extends StatefulWidget {
@override
_LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {

String message = "";

@override
Widget build(BuildContext context) {
return Container(
child: Column(
children: [
Text(message),
Flatbutton(
....
onPressed: (){
AuthService(verificationMessage: (String newMessage) => {
setState(() {
message = newMessage
})
})
}
)
]
)
);
}



}

关于firebase - 如何访问Future方法中存在的回调函数的返回值。 Dart ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61111892/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com