【问题标题】:Flutter: Add Authenticated Users to FirestoreFlutter:将经过身份验证的用户添加到 Firestore
【发布时间】:2020-04-08 15:24:00
【问题描述】:

我正在尝试从注册页面对新注册的用户进行身份验证,并在他们通过身份验证后将其添加到我的 cloud-firestore 数据库中。目前,用户已通过身份验证,并且代码在该阶段运行良好。但是,它们不会添加到 cloud-firestore 数据库中。请帮忙!

Container(
                    width: 150.0,
                    child: FlatButton(
                      child: Text('Sign Up'),
                      color: Colors.grey,
                      onPressed: () {
                        if (_registerFormKey.currentState.validate()) {
                          if (pwdInputController.text ==
                              confirmPwdInputController.text) {
                            FirebaseAuth.instance
                                .createUserWithEmailAndPassword(
                                    email: emailInputController.text,
                                    password: pwdInputController.text)
                                .then((currentUser) => Firestore.instance
                                    .collection("users")
                                    .document(currentUser.user.uid)
                                    .setData({
                                      "uid": currentUser.user.uid,
                                      "first-name":
                                          firstNameInputController.text,
                                      "last-name":
                                          lastNameInputController.text,
                                      "email": emailInputController.text,
                                      'password': pwdInputController.text,
                                    })
                                    .then((result) => {
                                          Navigator.pushAndRemoveUntil(
                                              context,
                                              MaterialPageRoute(
                                                  builder: (context) =>
                                                      MyApp()),
                                              (_) => false),
                                          firstNameInputController.clear(),
                                          lastNameInputController.clear(),
                                          emailInputController.clear(),
                                          pwdInputController.clear(),
                                          confirmPwdInputController.clear()
                                        })
                                    .catchError((err) => print(err)))
                                .catchError((err) => print(err));
                          } else {
                            showDialog(
                                context: context,
                                builder: (BuildContext context) {
                                  return AlertDialog(
                                    title: Text("Error"),
                                    content:
                                        Text("The passwords do not match"),
                                    actions: <Widget>[
                                      FlatButton(
                                        child: Text("Close"),
                                        onPressed: () {
                                          Navigator.of(context).pop();
                                        },
                                      )
                                    ],
                                  );
                                });
                          }
                        }
                      },
                    ),
                  ),

【问题讨论】:

  • 你有没有打印在控制台上的日志?
  • 不,我不知道。 :-(
  • 我还注意到它没有重定向到主页 - MyApp()
  • 打印firestore插入后的结果,看看返回值是什么
  • 我该怎么做?

标签: firebase flutter google-cloud-firestore


【解决方案1】:

您的代码在我的模拟器上运行良好,因此我会仔细检查以下内容,确保您已正确设置了 Firestore Documentation can be found here. 还要仔细检查您的数据库安全规则Documentation for security rules can be found here.

如果您的数据库仍处于测试阶段,您的规则将如下所示:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {

    // This rule allows anyone on the internet to view, edit, and delete
    // all data in your Firestore database. It is useful for getting
    // started, but it is configured to expire after 30 days because it
    // leaves your app open to attackers. At that time, all client
    // requests to your Firestore database will be denied.
    //
    // Make sure to write security rules for your app before that time, or else
    // your app will lose access to your Firestore database
    match /{document=**} {
      allow read, write: if request.time < timestamp.date(2020, 5, 5);
    }
  }
}

【讨论】:

  • 我更改了安全规则。我将“如果为假”更改为“如果为真”。但是,它仍然无法正常工作。
猜你喜欢
  • 2021-11-02
  • 2018-08-18
  • 2017-12-27
  • 2013-08-20
  • 2019-09-27
  • 2020-11-26
  • 2020-01-27
  • 2019-11-22
  • 2016-01-17
相关资源
最近更新 更多