'(BuildContext, Exception) => void' 不是类型 '(BuildContext, Exception) => () => void' 的子类型

问题描述 投票:0回答:2

我有一个凸起的按钮,它有一个闭包,可以在我的 AuthenticationProvider 中执行一个函数

RaisedButton(
  textColor: Theme.of(context).backgroundColor,
  onPressed: () => authenticationProvider.registerWithPhone(context, _phoneNumber, onFailed: _onRegistrationFailed),
  child: Text('Send'),
)

它传递一个在同一个小部件中定义的回调函数

  void _onRegistrationFailed(BuildContext context, Exception exception)
  {
    showDialog(
      context: context,
      builder: (context) {
        return PlatformAlertDialog('test', 'test', 'test');
    });
  }

但是我收到以下错误

'(BuildContext, Exception) => void' is not a subtype of type '(BuildContext, Exception) => () => void'

这是由凸起按钮调用的函数

  Future<void> registerWithPhone(BuildContext context, PhoneNumberModel phoneNumber,
      {VoidCallback onSuccess(BuildContext context), VoidCallback onFailed(BuildContext context, Exception exception)}) async {
    _status = AuthenticationStatus.Authenticating;
    notifyListeners();
    String localizedPhoneNumber = phoneNumber.toString();

    return await _authentication.verifyPhoneNumber(
        phoneNumber: localizedPhoneNumber,
        timeout: Duration(seconds: _phoneVerificationTimeout),
        verificationCompleted: (authCredential) => _verificationComplete(context, authCredential, onSuccess: onSuccess),
        verificationFailed: (authException) => _verificationFailed(context, authException, onFailed: onFailed),
        codeAutoRetrievalTimeout: (verificationId) => _codeAutoRetrievalTimeout(verificationId),
        codeSent: (verificationId, [code]) => _smsCodeSent(verificationId, [code]));
  }

那么我应该如何在闭包中传递回调呢?

flutter dart closures
2个回答
2
投票

实际问题是你的类型定义。您期望返回

VoidCallback
,而函数返回
void
(什么也没有)。

改变:

VoidCallback onFailed(BuildContext context, Exception exception)

void onFailed(BuildContext context, Exception exception)

void Function(BuildContext,Exception) onFailed

onSuccess
也一样。


0
投票

迈克,

您声明的参数存在问题:

 VoidCallback onFailed(BuildContext context, Exception exception)

意味着它是一个返回类型为VoidCallback的函数,当你传递函数时:

void _onRegistrationFailed(BuildContext context, Exception exception)

其返回类型为 void

要修复错误,您应该像这样重写函数:

Future<void> registerWithPhone(BuildContext context, PhoneNumberModel phoneNumber,
      {void onSuccess(BuildContext context), void onFailed(BuildContext context, Exception exception)}) async {
    _status = AuthenticationStatus.Authenticating;
    notifyListeners();
    String localizedPhoneNumber = phoneNumber.toString();

    return await _authentication.verifyPhoneNumber(
        phoneNumber: localizedPhoneNumber,
        timeout: Duration(seconds: _phoneVerificationTimeout),
        verificationCompleted: (authCredential) => _verificationComplete(context, authCredential, onSuccess: onSuccess),
        verificationFailed: (authException) => _verificationFailed(context, authException, onFailed: onFailed),
        codeAutoRetrievalTimeout: (verificationId) => _codeAutoRetrievalTimeout(verificationId),
        codeSent: (verificationId, [code]) => _smsCodeSent(verificationId, [code]));
  }

请注意,同样适用于 onSuccess 参数。

我还建议考虑使用函数类型别名:https://dart.dev/guides/language/language-tour#typedefs

© www.soinside.com 2019 - 2024. All rights reserved.