【问题标题】:The following ArgumentError was thrown building Home(dirty, state: _HomeState#75cb9):在构建 Home(dirty, state: _HomeState#75cb9) 时引发了以下 ArgumentError:
【发布时间】:2021-09-17 03:51:30
【问题描述】:

我已经使用 Firebase 身份验证设置了登录页面

我在我的代码中实现了一个_submit() 函数,该函数使用signInWithEmailAndPassword() 和一个单独的googleSignIn() 方法登录用户以使用google 登录

但是当我尝试使用_submit() 登录时,我得到了这个错误

The following ArgumentError was thrown building Home(dirty, state: _HomeState#75cb9)

这是我的_submit() 方法

 _submit() async {
final isValid = _key.currentState.validate();
if (isValid) {
  _key.currentState.save();
  EasyLoading.show(status: "Please Wait...\n" + "Loggin in with \n");
  try {
    UserCredential userCredential = await FirebaseAuth.instance
        .signInWithEmailAndPassword(email: email, password: password);

    assert(await user.getIdToken() != null);
    User currentUser = auth.currentUser;
    assert(user.uid == currentUser.uid);

    print(userCredential);
    EasyLoading.dismiss();
    EasyLoading.showSuccess("Welcome back");

    Navigator.of(context)
        .push(MaterialPageRoute(builder: (context) => Home()));
        
  } on FirebaseAuthException catch (e) {
    EasyLoading.dismiss();
    if (e.code == 'user-not-found') {
      Alert(
        context: context,
        style: AlertStyle(
          backgroundColor: bgcolor,
          titleStyle: TextStyle(
              fontFamily: 'valorant',
              fontWeight: FontWeight.bold,
              color: Colors.white),
          descStyle: TextStyle(
              fontFamily: 'valorant', fontSize: 15, color: Colors.white),
        ),
        type: AlertType.error,
        title: "Account Was Not Found !!",
        desc: "the email id entered does not exist ",
        buttons: [
          DialogButton(
            color: Colors.red,
            child: Text(
              "Create Account",
              style: TextStyle(
                  color: Colors.white,
                  fontSize: 20,
                  fontFamily: 'Valorant'),
            ),
            onPressed: () async {
              Navigator.of(context).pop();
              Navigator.of(context)
                  .push(MaterialPageRoute(builder: (context) => SignUP()));
            },
            width: 200,
          )
        ],
      ).show();
      print('No user found for that email.');
    } else if (e.code == 'wrong-password') {
      print('Wrong password provided for that user.');
    }
  }
  }
 }

用户要去的类是这样的

class Home extends StatefulWidget {
const Home({Key key}) : super(key: key);

@override
_HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
bool isLoggedIn = true;
FirebaseAuth _auth = FirebaseAuth.instance;
User _user = FirebaseAuth.instance.currentUser;
@override
Widget build(BuildContext context) {


void signout() async {
  await FirebaseAuth.instance.signOut();
  setState(() {
    isLoggedIn = false;
  });
  Timer(Duration(seconds: 2), () {
    Navigator.of(context).pushAndRemoveUntil(
        MaterialPageRoute(builder: (context) => SignIN()),
        (route) => false);
  });
}

@override
void initState() {
  super.initState();
  FirebaseAuth auth = FirebaseAuth.instance;
  auth.authStateChanges().listen((user) {
    if (user == null) {
      print("no user Logged In");
      return isLoggedIn = false;
    } else {
      print(user.displayName + " is signed in ");

      return isLoggedIn = true;
    }
  });
}

return isLoggedIn
    ? Scaffold(
        appBar: AppBar(
          title: Text("HI " + _user.displayName),
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: signout,
            child: Text("Log Out"),
          ),
        ),
      )
    : Scaffold(
        body: Center(
          child: Text("NO USER LOGGED IN"),
        ),
      );
   }
 }

我得到的错误是

The relevant error-causing widget was
Home                                           lib\Authentication\signin.dart:59
When the exception was thrown, this was the stack
#0      _StringBase.+ (dart:core-patch/string_patch.dart:272:57)
#1      _HomeState.build                       package:myapp/home/Home.dart:62
#2      StatefulElement.build                  package:flutter/…/widgets/framework.dart:4775
#3      ComponentElement.performRebuild        package:flutter/…/widgets/framework.dart:4658
#4      StatefulElement.performRebuild

我有一个谷歌登录功能,可以毫无问题地将我带到主页 但是当使用电子邮件 ID 和密码时,我会遇到此错误

我的谷歌登录方式是:

 signInWithGoogle() async {
EasyLoading.show(
    status: "Please Wait...", maskType: EasyLoadingMaskType.black);

GoogleSignInAccount googleSignInAccount = await _googleSignIn.signIn();
GoogleSignInAuthentication googleSignInAuthentication =
    await googleSignInAccount.authentication;
AuthCredential credential = GoogleAuthProvider.credential(
  accessToken: googleSignInAuthentication.accessToken,
  idToken: googleSignInAuthentication.idToken,
);

UserCredential userCredential = await auth.signInWithCredential(credential);
user = userCredential.user;
assert(!user.isAnonymous);
assert(await user.getIdToken() != null);

User currentUser = auth.currentUser;
assert(user.uid == currentUser.uid);
EasyLoading.dismiss();
EasyLoading.showSuccess("Welcome Back !!",
    maskType: EasyLoadingMaskType.black, duration: Duration(seconds: 3));

Navigator.of(context).pushAndRemoveUntil(
    MaterialPageRoute(builder: (context) => Home()), (route) => false);

print(user.displayName);
print(user.email);
}

这导致

在哪里

没问题

【问题讨论】:

  • 你的signin.dart 文件的第 59 行有什么内容?
  • return isLoggedIn 在我提供的代码上看到这个
  • 您能发布完整的signin.dart 文件吗?
  • @VictorEronmosele 她是我的 signin.dart 文件的链接
  • @VictorEronmosele 这里是signin.dart的链接link

标签: flutter firebase-authentication


【解决方案1】:

initState 方法和signOut 方法应该在您的build 方法之外。

class Home extends StatefulWidget {
  const Home({Key key}) : super(key: key);

  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  ...

  void signout() async {
    ...
  }

  @override
  void initState() {
    ...
  }

  @override
  Widget build(BuildContext context) {
    ...
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-10
    • 1970-01-01
    • 2021-09-18
    • 1970-01-01
    相关资源
    最近更新 更多