首页 > 解决方案 > 在 null (Firebase) Flutter 上调用方法

问题描述

有时,当我尝试使用 firebase 进行登录时,会出现以下错误:

W/DynamiteModule(8142):找不到 com.google.firebase.auth 的本地模块描述符类。

I/FirebaseAuth(8142): [FirebaseAuth:] 准备创建与 gms 实施的服务连接

D/FirebaseAuth(8142):通知 id 令牌侦听器有关用户 (skEEmC0dDBW9z73MsfShWX8M1iu1)。

I/flutter(8142):错误:NoSuchMethodError:方法“调用”在 null 上调用。

I/颤振(8142):接收器:空

I/flutter(8142):尝试调用:call()

特别是如果我单击注册按钮返回登录。有人可以解释为什么我会收到此错误以及如何解决它:这是代码:

     class LoginPage extends StatefulWidget{

      static const routeName = '/login';

       LoginPage({this.onSignedIn});

        final VoidCallback onSignedIn;

         @override
          State<StatefulWidget> createState() => new _LoginPageState();

                    }

          enum FormType {
             login,
            }

         class _LoginPageState extends State <LoginPage> {

          final formKey = new GlobalKey<FormState>();

           String _email,_password;

           bool validateAndSave(){
           final form = formKey.currentState;
            if(form.validate()){
            form.save();
            
          return true;
           }
          return false;
          }

    void validateAndSubmit() async{
     if(validateAndSave()){
   try{
   await FirebaseAuth.instance.signInWithEmailAndPassword(email: _email, password: _password);
    widget.onSignedIn();
  }catch (e){
  print('Error:$e');
  }
  }
 }

   void moveToRegister() {
    Navigator.of(context).pushReplacementNamed(RegisterPage.routeName);
     }

    @override
   Widget build(BuildContext context) {
return Scaffold(
  appBar: new AppBar(
    title:new Text ('',
    style: new TextStyle(color:Colors.white, fontWeight:FontWeight.bold)
  ),
  backgroundColor: Color(0xff04346c),
  ),
  body: Stack (
    children:<Widget>[
      Container(
        decoration: BoxDecoration(
        gradient: LinearGradient(
        colors: [
          Color(0xff04a3b3),
          Color(0xff04a3b3),
          Color(0xff04a3b3),
          Color(0xff04a3b3),
        ]
      )
    ),
  ),
    Center(
      child: Card(
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(10.0)
      ),
      child: Container(
        height:350,
        width:350,
        padding: EdgeInsets.all(16.0),
        child: Form(
          key:formKey,
          child: new Column( 
          children: 
          buildInputs() + buildSubmitButtons(),
          )
        )
     )
    )
   )
  ],
  ),
 );
}

List<Widget> buildInputs(){
return [
   Image.asset('imageapp/myimage.png',height:50,width: 250,),
   new TextFormField( 
          decoration: new InputDecoration(labelText: 'Email',
          icon: new Icon (Icons.email)
          ),
          onSaved: (value) => _email = value,
          ),
          new TextFormField(
            decoration: new InputDecoration (labelText: 'Password',
          icon: new Icon (Icons.lock)
          ),
            obscureText: true,
            onSaved: (value) => _password = value,
          ),
    ];
}

List<Widget> buildSubmitButtons(){
 return [
  Padding(padding: EdgeInsets.all(3.0)),
  new RaisedButton(
            child: new Text('Login',style: new TextStyle(fontSize:18.0,color: Colors.white),
            ),
            color:  Color(0xff04346c),
            shape: RoundedRectangleBorder(
            borderRadius : new BorderRadius.circular(30.0)
            ),
            onPressed: validateAndSubmit
            ),
            new FlatButton(
              onPressed: moveToRegister, 
              child: new Text('Create an account',style: new TextStyle(fontSize:13.0),)
              )
       ];
    }
    }

标签: androidfirebasefluttermobile

解决方案


问题出在这个

widget.onSignedIn();

我将其更改为 Navigate....(到仪表板页面)


推荐阅读