首页 > 解决方案 > method call of another class from a widget supposed to return true, but returns nothing

问题描述

I have this method below which resides in a class, I am trying to call this method from a widget using Authenticate().signInWithPhone, and based on its return value I am supposed to setState of a variable inside of the widget class which in turn changes the screen elements. But when running this code I believe nothing is being run inside of the setState method, because the codeSent variable always has the false value.

class Authenticate {
  String verificationId = '';
  String phone = '';
  bool codeSent = false;

  Future<User?> getSessionStatus() async {
    return await FirebaseAuth.instance.currentUser;
  }

  Future<bool> signInWithPhone(String phone, BuildContext context) async {
    await FirebaseAuth.instance.verifyPhoneNumber(
      phoneNumber: phone,
      verificationCompleted: (PhoneAuthCredential credential) async {
        await FirebaseAuth.instance.signInWithCredential(credential);
        final snackBar = SnackBar(content: Text("Login Success"));
        ScaffoldMessenger.of(context).showSnackBar(snackBar);
      },
      verificationFailed: (FirebaseAuthException e) {
        final snackBar = SnackBar(content: Text("${e.message}"));
        ScaffoldMessenger.of(context).showSnackBar(snackBar);
      },
      codeSent: (String verificationId, int? resendToken) {
        codeSent = true;
        verificationId = verificationId;
      },
      codeAutoRetrievalTimeout: (String verificationId) {
        verificationId = verificationId;
      },
      timeout: Duration(seconds: 60),
    );
    return codeSent;
  }
}

Now I am calling this method from a stateful widget.

            ElevatedButton(
              onPressed: () {
                //verifyPhone();
                Authenticate()
                    .signInWithPhone(phone, context)
                    .then((result) => {
                          print(result),
                          setState(() {
                            codeSent = result;
                          }),
                          print(result),
                        });
              },
              child: Text('Verify'),
            )

标签: firebaseflutterdartfirebase-authentication

解决方案


推荐阅读