title: Phone authentication with firebase and flutter
body: <p>I'm writing a flutter app that using phone authentication. I am runing the app currently on iPhone 12 simulator and when I testing the app and implement the phone number I am not receiving the 6 digit code from firebase as planed. I Think is because one of those mention resons.</p>
  <ul>
  <li>
  <ol>
  <li>Firebase can't send the code to the simulator with fake number.</li>
  </ol>
  </li>
  <li>
  <ol start="2">
  <li>I need to change some settings on firebase/flutter project so it will work as expected.</li>
  </ol>
  </li>
  <li>
  <ol start="3">
  <li>I need to send the phone number with the area code as well.</li>
  </ol>
  </li>
  <li>
  <ol start="4">
  <li>my code is wrong or not doing what I want it to do.</li>
  </ol>
  </li>
  </ul>
  <p>(I don't have an apple developer account yet, so I gave a test number and code to firebase so when I will put the fake number I will still get the code to the app (not as push notification)).</p>
  <p><strong>Some dots of the app:</strong></p>
  <p>I have no problem to test the app on real device if needed.<br />
  I don't need to get the push notification right now, I can just print the verification code to the console so I will see that the code work.<br />
  I want to start the verification process right after I sent the phone number to this page (verification page)<br />
  right now I am using a full number without the area code.</p>
  <p>I can use a fake number for the simulator? or I need a real one?.</p>
  <p>Thank you, and hope you can help e with my problem.</p>
  <p>this is my code (only the verification part):</p>
  <pre><code>// imports are here
  
  enum Status { waiting, error }
  
  class VerificationCode extends StatefulWidget {
    const VerificationCode({Key? key, this.number}) : super(key: key);
    final number;
    @override
    _VerificationCodeState createState() =&gt; _VerificationCodeState(number);
  }
  
  class _VerificationCodeState extends State&lt;VerificationCode&gt; {
    late final phoneNumber;
    final _verKey = GlobalKey&lt;FormState&gt;();
    late String _verCode;
    late double _formHeight;
    final FirebaseAuth _auth = FirebaseAuth.instance;
    var _verificationId;
    var _status = Status.waiting;
    _VerificationCodeState(this.phoneNumber); // storing the phone number from other page
  
    @override
    void initState() {
      super.initState();
      _verifyPhoneNumber();
    }
  
    Future _verifyPhoneNumber() async {
      _auth.verifyPhoneNumber(
          phoneNumber: phoneNumber,
          verificationCompleted: (phonesAuthCredentials) async {},
          verificationFailed: (verificationFailed) async {},
          codeSent: (verificationId, reseningToken) async {
            setState(() {
              _verificationId = verificationId;
              print(_verificationId); // here I am printing the opt code so I will know what it is to use it
            });
          },
          codeAutoRetrievalTimeout: (verificationId) async {});
    }
  
    Future _sendCodeToFirebase({String? code}) async {
      if (_verificationId != null) {
        var credential = PhoneAuthProvider.credential(
          verificationId: _verificationId,
          smsCode: code!,
        );
  
        await _auth
            .signInWithCredential(credential)
            .then((value) {
              print(&quot;auth complete!&quot;);
            })
            .whenComplete(() {})
            .onError((error, stackTrace) {
              setState(() {
                _verKey.currentState!.reset();
                _status = Status.error;
              });
            });
      }
    }
  
    @override
    Widget build(BuildContext context) {
      Size size = MediaQuery.of(context).size;
      if (size.height &lt;= 736) {
        _formHeight = (size.height * .05) + 6;
      } else {
        _formHeight = size.height * .048;
      }
      return Scaffold(
        body: SafeArea(
          child: GestureDetector(
            onTap: () =&gt; FocusScope.of(context).requestFocus(
              FocusNode(),
            ),
            child: SingleChildScrollView(
              child: ConstrainedBox(
                constraints: BoxConstraints(
                  minHeight: size.height - 90,
                ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  children: [
                    Stack(
                      alignment: Alignment.topLeft,
                      children: [
                        GestureDetector(
                          onTap: () {
                            Navigator.pop(context);
                          },
                          child: Padding(
                            padding: EdgeInsets.only(
                              top: size.height * .006,
                              left: size.width * .03,
                            ),
                            child: SvgPicture.asset(
                              &quot;assets/arrow-back.svg&quot;,
                            ),
                          ),
                        ),
                      ],
                    ),
                    Padding(
                      padding: EdgeInsets.only(
                        top: size.height * .18,
                      ),
                      child: Center(
                        child: Text(
                          &quot;AppName&quot;,
                          style: TextStyle(
                            fontSize: size.width * .096,
                            letterSpacing: size.width * .026,
                            fontWeight: FontWeight.w300,
                          ),
                        ),
                      ),
                    ),
                    Padding(
                      padding: EdgeInsets.only(top: size.height * .015),
                      child: Center(
                        child: Container(
                          margin: EdgeInsets.symmetric(
                            horizontal: size.width * .045,
                          ),
                          height: _formHeight,
                          child: Form(
                            key: _verKey,
                            child: TextFormField(
                              style: TextStyle(
                                fontSize: size.width * .035,
                              ),
                              decoration: InputDecoration(
                                counterText: &quot;&quot;,
                                contentPadding:
                                    const EdgeInsets.fromLTRB(0, 10, 0, 0),
                                hintText: 'Verification Code',
                                enabledBorder: OutlineInputBorder(
                                  borderRadius: BorderRadius.circular(7.0),
                                  borderSide: const BorderSide(
                                    color: Colors.black,
                                    width: 1.25,
                                  ),
                                ),
                                focusedBorder: OutlineInputBorder(
                                  borderRadius: BorderRadius.circular(7.0),
                                  borderSide: const BorderSide(
                                    color: Colors.black,
                                    width: 1.5,
                                  ),
                                ),
                              ),
                              autofocus: false,
                              keyboardType: TextInputType.number,
                              cursorColor: Colors.black,
                              textAlign: TextAlign.center,
                              onChanged: (input) async {
                                if (input.length == 6) {
                                  _verCode = input;
                                  _sendCodeToFirebase(code: _verCode);
                                }
                              },
                              maxLength: 6,
                            ),
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
        ),
      );
    }
  }
  </code></pre>