Flutter Firebase Authentication Troubles

Asked 2 years ago, Updated 2 years ago, 80 views

Prerequisites/What you want to achieve

I'd like to authenticate my phone number using Firebase Authentication on the Flutter app.

Problems/Error Messages you are experiencing

I am using Android studio.
When you press the authentication button,

Xcode build done.21.5s
    path: satified (Path is satified), interface: en0
Configuring the default Firebase app...
Configured the default Firebase app__FIRAPP_DEFAULT.
    path: satified (Path is satified), interface: en0
Syncing files to device iPhone 11 Pro Max...
*** First throw call stack:
(
    0 CoreFoundation 0x00007ffff23c7127e__exceptionPreprocess+350
    1 libobjc.A.dylib0x00007ffff513fbb20objc_exception_throw+48
    2 CoreFoundation 0x00007ffff 23c710bc + [NSException raise:format:] +188
    3 Runner 0x0000000103e97d81 - [FIRPhoneAuthProvider verifyPhoneNumber:UIDelegate: completion:] +193
    4 Runner 0x000000010440dc3d - [FLTFirebaseAuthPlugin handleMethodCall: result:] +18109
    5Flutter0x000000010600ddb5_45 - [FlutterMethodChannel setMethodCallHandler:]_block_invoke+104
    6 Flutter 0x0000000105fa1ba0_ZNK7flutter21 PlatformMessageRouter21 HandlePlatf<...>
Lost connection to device.

An error similar to the one shown in

My own code

import'package:flutter/material.dart';

import 'package:firebase_auth/firebase_auth.dart';


void main() = > runApp(MyApp());

class MyApp extensions StatelessWidget {
  @override
  Widget build (BuildContext context) {
    US>return MaterialApp(
      home —MyHomePage(),

//      initialRoute: '/',
//      routes: {
//        ' / ' : (context) = > WelcomeClass(),
//        '/LoginClass': (context) = > LoginClass(),
//        '/SignUpClass': (context)=>SignUpClass(),
//      },
    );
  }
}

class MyHomePage extensions StatefulWidget{
  @override
  _MyHomePageState createState()=>_MyHomePageState();
}

class_MyHomePageState extensions State<MyHomePage>{
  String phoneNo;
  String smsCode;
  String verificationId;

  Future<void>verifyPhone() async {
    finalPhoneCodeAutoRetrievalTimeout autoRetrieve=(String verId){
      This.verificationId=verId;
    };

    final PhoneCodeSent smsCodeSent=(String verId, [int forceCodeResend]) {
      This.verificationId=verId;
      smsCodeDialog(context).then(value){
        print('signed in');
      });
    };

    finalPhoneVerificationCompleted verificationSuccess=(AuthCredential user){
      print('Phone Verification Completed');
    };

    finalPhoneVerificationFailed verificationFailed=
        (AuthException exception) {
      print('${exception.message}');
    };

    wait FirebaseAuth.instance.verifyPhoneNumber(
      phoneNumber —this.phoneNo,
      codeAutoRetrievalTimeout:autoRetrieve,
      codeSent —smsCodeSent,
      timeout:const Duration (seconds:60),
      verificationCompleted:verificationSuccess,
      verificationFailed —verificationFailed,
    );
  }

  Future<bool>smsCodeDialog (BuildContext context) {
    return showDialog(
        context,
        barrierDismissable: false,
        builder: (BuildContext context) {
          US>return new AlertDialog(
            title:Text('Enter sms code'),
            content —TextField(
              onChanged: (value) {
                This.smsCode=value;
              },
            ),
            contentPadding —EdgeInsets.all(10),
            actions:<Widget>[
              FlatButton(
                child:Text('Done',
                onPressed:() {
                  FirebaseAuth.instance.currentUser().then(user){
                    if(user!=null){
                      Navigator.of(context).pop();
                      Navigator.of(context)
                          .pushReplacementNameed('/SignUpClass');
                    } else{
                      Navigator.of(context).pop();
                      signIn();
                    }
                  });
                },
              )
            ],
          );
        });
  }

  signIn(){
    finalAuthCredentialCredential=PhoneAuthProvider.getCredential(
        verificationId:verificationId, smsCode:smsCode);

    FirebaseAuth.instance.signInWithCredential(credential).then(user){
      Navigator.of(context).pushReplacementNamed('/homepage');
    }).catchError(e){
      print('Auth Credential Error:$e');
    });
  }
  @override
  Widget build (BuildContext context) {
    US>return Scaffold(
      body —Padding(
        padding —const EdgeInsets.all (38.0),
        child —Column(
          crossAxisAlignment —CrossAxisAlignment.start,
          children:<Widget>[
            TextField(
              onChanged: (value) {
                This.phoneNo=value;
              },
            ),
            SizedBox(
              height:40,
            ),
            FlatButton(
              child: Text ("Send code"),
              onPressed —verifyPhone,
            , // FlatButton
          ], // Widget
        ),
      ), // Column
    );
  }
}



I'm really in trouble.Thank you for your cooperation.

Supplementary information (for example, FW/Tool Version)

  • Android Studio
  • Dart
  • Firebase Authentication

multipost destination:

android android-studio firebase flutter

2022-09-29 21:39

1 Answers

I think I forgot about Google Service-Info.plist or I'm wrong.
This doesn't matter if it's phone number authentication, it's important to first enter the application key number before downloading GoogleService-Info.plist, depending on the situation
Could not connect to firebase
Please look at Google's explanation and set it up again


2022-09-29 21:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.