为什么Flutter尝试登出应用程式时会在控制台中引发NoSuchMethodError,即使该程式在模拟器中也能运作?

时间:2019-07-19 12:45:02

标签: flutter firebase-authentication flutter-navigation

我正在为Flutter应用设置身份验证流程,并且模拟器中的所有操作均正常进行。 SignUp运作良好,SignOut也运作良好。尽管在模拟器中进行了操作,但flutter在尝试注销时仍会引发NoSuchMethod错误。

我的注销屏幕

module.exports = {
    module: {
        rules: [
            {
                test: /\.css$/i,
                use: ['style-loader', 'css-loader'],
            },
            { test: /\.jsx?$/, exclude: /node_modules/,
                use: {
                    loader: 'babel-loader'
                }
            }
        ]
    }
}

注册中的相关代码

class Logout extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: ReusableCard(
          margin: EdgeInsets.all(20.0),
          cardChild: Padding(
            padding: EdgeInsets.all(100.0),
            child: ReusableCard(
              cardChild: Text('Log out'),
              hexcolor: Colors.blueAccent,
              onPress: () async {
                await AuthService().signOut();
                Navigator.pushNamedAndRemoveUntil(
                    context, '/registration', (route) => false);
              },
            ),
          ),
        ),
      ),
    );
  }
}

错误消息

 void initState() {
    super.initState();
    auth.getUser.then(
      (user) {
        if (user != null) {
          Navigator.pushReplacementNamed(context, '/proddy');
        }
      },
    );
  }

                    FlatButton(
                      child: Text('Sign Up'),
                      onPressed: () async {
                        emailValid = validator.email(email);
                        passwordValid = validator.password(password);
                        if (emailValid == true && passwordValid == true) {
                          FirebaseUser user = await auth.createUserWithEmail(
                            email: email,
                            password: password,
                          );

                          if (user != null) {
                            await auth.updateUserData(user);
                            Navigator.pushReplacementNamed(context, '/start');
                          }
                        } else {
                          print('Your credentials are invalid');
                        }
                      },
                      color: Colors.greenAccent,
                      shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(20.0)),
                    ),

与错误有关的代码

flutter: ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY 
flutter: The following NoSuchMethodError was thrown building Start(dirty, dependencies: [MediaQuery,
flutter: InheritedProvider<FirebaseUser>]):
flutter: The getter 'uid' was called on null.
flutter: Receiver: null
flutter: Tried calling: uid

感谢您的任何帮助!我真的很感激,因为我被困在这里:)

1 个答案:

答案 0 :(得分:1)

请尝试放置auth而不是AuthService(),因为auth是FirebaseAuth的实例,您应该从中注销。 FirebaseAuth auth = FirebaseAuth.instance;

onPress: () async {
   await auth.signOut();
   Navigator.pushNamedAndRemoveUntil(
   context, '/registration', (route) => false);
},