错误:NoSuchMethodError:类“ _AssertionError”没有实例获取器“代码”

时间:2020-09-04 12:03:44

标签: firebase flutter firebase-authentication

当我尝试使用firebaseauth登录时,我总是收到错误提示

Picture Of Error

我一直在网上关注有关如何执行此操作的教程。但这似乎仍然对我不起作用。

这是我的代码:

import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';

bool isAuth = false;

FirebaseAuth _auth = FirebaseAuth.instance;
final googleSignIn = GoogleSignIn();

Future<bool> signUp({String email, String password}) async {
  try {
    User user = (await _auth.createUserWithEmailAndPassword(email: email, password: password)).user;
    return Future.value(true);
  } catch (e) {
    switch (e.code) {
      case "email-already-in-use":
        print("email-already-in-use");
    }
  }
}

Future<bool> signIn({String email, String password}) async {
  try {
    User user = (await _auth.signInWithEmailAndPassword(email: email, password: password)).user;
    return Future.value(true);
  } catch (e) {
    switch (e.code) {
      case "user-not-found":
        print("user-not-found");
        break;
    }
  }
}

Future<bool> signOutUser() async {
  User user = await _auth.currentUser;
  if (user.providerData[1].providerId == "google.com") {
    await googleSignIn.signOut();
  }
  _auth.signOut();
  return Future.value(true);
}

1 个答案:

答案 0 :(得分:0)

这样定义您的try catch块:

try {
    User user = (await _auth.createUserWithEmailAndPassword(email: email, password: password)).user;
    return Future.value(true);
  } on FirebaseAuthException catch (e) {
    switch (e.code) {
      case "email-already-in-use":
        print("email-already-in-use");
return null;
    }
  }
}

  try {
    User user = (await _auth.signInWithEmailAndPassword(email: email, password: password)).user;
    return Future.value(true);
  }on FirebaseAuthException catch (e) {
    switch (e.code) {
      case "user-not-found":
        print("user-not-found");
        break;
return null;
    }
  }