Home > Blockchain >  Unhandled Exception: type '(String, int) => Future<Null>' is not a subtype of typ
Unhandled Exception: type '(String, int) => Future<Null>' is not a subtype of typ

Time:10-21

Need help to solve this problem, i got stuck with this when running my code. it keeps on showing this error Unhandled Exception: type '(String, int) => Future' is not a subtype of type '(String, int?) => void' in type cast.. i tried solving this to no avail, i went to various forums and i saw different ideas which didnt work for. i hope someone can help me out here.

class AuthProvider with ChangeNotifier {

  FirebaseAuth _auth = FirebaseAuth.instance;
  late String smsOtp;
  late String verificationId;
  String error = '';
  UserServices _userServices = UserServices();

  Future < void > verifyPhone(BuildContext context, String number) async {
    final PhoneVerificationCompleted verificationCompleted =
      (PhoneAuthCredential credential) async {
        await _auth.signInWithCredential(credential);
      };

    final PhoneVerificationFailed verificationFailed = (FirebaseAuthException e) {
      print(e.code);
    };

    final PhoneCodeSent smsOtpSend = (String verId, int resendToken) async {
      this.verificationId = verId;
    }
    as PhoneCodeSent;

    smsOtpDialog(context, number);

    try {
      _auth.verifyPhoneNumber(
        phoneNumber: number,
        verificationCompleted: verificationCompleted,
        verificationFailed: verificationFailed,
        codeSent: smsOtpSend,
        codeAutoRetrievalTimeout: (String verId) {
          this.verificationId = verId;
        },
      );

    } catch (e) {
      print(e);
    }
  }

  Future < bool ? > smsOtpDialog(BuildContext context, String number) {
    return showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Column(
            children: [
              Text('Verification Code'),
              SizedBox(height: 6, ),
              Text('Enter the 6 didgit Code received by sms',
                style: TextStyle(color: Colors.grey, fontSize: 10),
              ),
            ],
          ),
          content: Container(
            height: 85,
            child: TextField(
              textAlign: TextAlign.center,
              keyboardType: TextInputType.number,
              maxLength: 6,
              onChanged: (value) {
                this.smsOtp = value;
              },
            ),
          ),
          actions: [
            FlatButton(
              onPressed: () async {
                (context);
                try {
                  PhoneAuthCredential phoneAuthCredential =
                    PhoneAuthProvider.credential(
                      verificationId: verificationId,
                      smsCode: smsOtp,

                    );

                  final User ? user = (await _auth.signInWithCredential(phoneAuthCredential)).user;

                  //create user data in fireStore after successful login
                  _createUser(id: user!.uid, number: user.phoneNumber ? ? "");
                  //navigate to homepage after login
                  if (user != null) {
                    Navigator.of(context).pop();

                    //dont want to come back to welcome screen after login
                    Navigator.of(context).pushReplacement(MaterialPageRoute(
                      builder: (context) => HomeScreen(),
                    ));
                  } else {
                    print('Login Failed');
                  }

                } catch (e) {
                  this.error = 'Invalid Code';
                  print(e.toString());
                  notifyListeners();
                  Navigator.of(context).pop();
                }
              },
              child: Text('DONE', style: TextStyle(color: Theme.of(context).primaryColor), ),
            ),
          ],
        );
      });
  }

  void _createUser({
    required String id,
    required String number
  }) {
    _userServices.createUserData({
      'id': id,
      'number': number,
    });
  }
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

change

final PhoneCodeSent smsOtpSend = (String verId, int resendToken) async {
      this.verificationId = verId;
    }

to

final PhoneCodeSent smsOtpSend = (String verId, int? resendToken) {
      this.verificationId = verId;
    }

the callback type is int? not int, note the question mark for null safety. And remove the async keyword because it's a regular function, not a future.

  • Related