0

I have a login screen with firebase phone authentication, I'm getting error " Unhandled Exception: type 'User' is not a subtype of type 'FirebaseUser' " when correct OTP is entered.

I have previously updated AuthResult result = await _auth.signInWithCredential(credential); to UserCredential result = await _auth.signInWithCredential(credential);

Any help will be highly appreciated.

flutter: Phone Controller:
[VERBOSE-2:profiler_metrics_ios.mm(184)] Error retrieving thread information: (ipc/send) invalid destination port
[VERBOSE-2:ui_dart_state.cc(177)] Unhandled Exception: type 'User' is not a subtype of type 'FirebaseUser'
#0      LoginScreen.loginUser.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:nobleapp/mscreen/mlogin.dart:61:38)
<asynchronous suspension>
#1      LoginScreen.loginUser.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:nobleapp/mscreen/mlogin.dart)
#2      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:993:19)
#3      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:1111:38)
#4      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:183:24)
#5      TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:598:11)
#6      BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:287:5)
#7      BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:222:7)
#8      PrimaryPointerGestureRecognizer.handleEvent (<…>
Application finished.

This is my full code

import 'package:firebase_auth/firebase_auth.dart';
import 'package:nobleapp/mscreen/homescreen.dart';
import 'package:flutter/material.dart';

class LoginScreen extends StatelessWidget {
  final _phoneController = TextEditingController();
  final _codeController = TextEditingController();
  final _passController = TextEditingController();
  Future<bool> loginUser(String phone, BuildContext context) async{
    FirebaseAuth _auth = FirebaseAuth.instance;

    _auth.verifyPhoneNumber(
        phoneNumber: phone,
        timeout: Duration(seconds: 60),
        verificationCompleted: (AuthCredential credential) async{
          Navigator.of(context).pop();

          UserCredential result = await _auth.signInWithCredential(credential);

          FirebaseUser user = result.user;

          if(user != null){
            Navigator.push(context, MaterialPageRoute(
                builder: (context) => HomeScreen(user: user,)
            ));
          }else{
            print("Error");
          }

          //This callback would gets called when verification is done auto maticlly
        },
        verificationFailed: (FirebaseAuthException exception){
          print(exception);
        },
        codeSent: (String verificationId, [int forceResendingToken]){
          showDialog(
              context: context,
              barrierDismissible: false,
              builder: (context) {
                return AlertDialog(
                  title: Text("Give the code?"),
                  content: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      TextField(
                        controller: _codeController,
                      ),
                    ],
                  ),
                  actions: <Widget>[
                    FlatButton(
                      child: Text("Confirm"),
                      textColor: Colors.white,
                      color: Colors.blue,
                      onPressed: () async{
                        final code = _codeController.text.trim();
                        AuthCredential credential = PhoneAuthProvider.getCredential(verificationId: verificationId, smsCode: code);
                        print('675565');
                        UserCredential result = await _auth.signInWithCredential(credential);
print('he617 result: $result');
                        FirebaseUser user = result.user;
                        print('676576');
                        if(user != null){
                          Navigator.push(context, MaterialPageRoute(
                              builder: (context) => HomeScreen(user: user,)
                          ));
                        }else{
                          print("Error");
                        }
                      },
                    )
                  ],
                );
              }
          );
        },
        codeAutoRetrievalTimeout: (String verificationId){
          print('VerificationId: $verificationId');
        }
    );
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Container(
          padding: EdgeInsets.all(32),
          child: Form(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text("Login", style: TextStyle(color: Colors.lightBlue, fontSize: 36, fontWeight: FontWeight.w500),),

                SizedBox(height: 16,),

                TextFormField(
                  decoration: InputDecoration(
                      enabledBorder: OutlineInputBorder(
                          borderRadius: BorderRadius.all(Radius.circular(8)),
                          borderSide: BorderSide(color: Colors.grey[200])
                      ),
                      focusedBorder: OutlineInputBorder(
                          borderRadius: BorderRadius.all(Radius.circular(8)),
                          borderSide: BorderSide(color: Colors.grey[300])
                      ),
                      filled: true,
                      fillColor: Colors.grey[100],
                      hintText: "Phone Number"

                  ),
                  controller: _phoneController,
                ),

                SizedBox(height: 16,),

                TextFormField(
                  decoration: InputDecoration(
                      enabledBorder: OutlineInputBorder(
                          borderRadius: BorderRadius.all(Radius.circular(8)),
                          borderSide: BorderSide(color: Colors.grey[200])
                      ),
                      focusedBorder: OutlineInputBorder(
                          borderRadius: BorderRadius.all(Radius.circular(8)),
                          borderSide: BorderSide(color: Colors.grey[300])
                      ),
                      filled: true,
                      fillColor: Colors.grey[100],
                      hintText: "Password"

                  ),

                  controller: _passController,
                ),

                SizedBox(height: 16,),

                Container(
                  width: double.infinity,
                  child: FlatButton(
                    child: Text("Login"),
                    textColor: Colors.white,
                    padding: EdgeInsets.all(16),
                    onPressed: (){
                      //code for sign in
                      // Place B
                      print('Phone Controller: ${_phoneController.text}');
                      final phone = _phoneController.text.trim();

                      loginUser('+974 12345678', context);
                    },
                    color: Colors.blue,
                  ),
                )
              ],
            ),
          ),
        )
    );
  }
}
Sulfy
  • 239
  • 4
  • 17

1 Answers1

1

replace verificationCompleted with this code and also change in HomeScreen argument.

 verificationCompleted: (AuthCredential credential) async{
      Navigator.of(context).pop();

      UserCredential result = await _auth.signInWithCredential(credential);

      User user = result.user;

      if(user != null){
        Navigator.push(context, MaterialPageRoute(
            builder: (context) => HomeScreen(user: user,)
        ));
      }else{
        print("Error");
      }

      //This callback would gets called when verification is done auto maticlly
    },
xbadal
  • 1,284
  • 2
  • 11
  • 24