【问题标题】:recovery user ID with flutter用颤振恢复用户ID
【发布时间】:2021-06-11 12:40:13
【问题描述】:

我的重定向有问题。 如果我的用户是我的 registerscreen 的新用户,我想重定向我的用户,如果它不是我的 topnavigationscreen 的新用户。

为了检查用户是否存在,我想从我的注册屏幕中创建的“cloud firestore”获取一些信息。 但我无法让他们回来。

我的小代码:

 void registergoogleUser() async {
    await _userProvider
        .registergoogleUser( _scaffoldKey)
        .then((response) async {
      if (response is Success<UserCredential>) {
        Future<DocumentSnapshot> getUser(String userId) async {
          final FirebaseFirestore instance = FirebaseFirestore.instance;
          print (await instance.collection('users')?.doc(userId)?.get());// for test
          (await instance.collection('users')?.doc(userId)?.get());
        }
        print (getUser);
        if (await getUser != null) {
          Navigator.of(context).pushNamedAndRemoveUntil(
              TopNavigationScreen.id, (route) => false);
        } else {
          Navigator.pop(context);
          Navigator.pushNamed(context, RegisterScreen.id);
        }
        }});
  }

但我的“运行”返回:

[ERROR:flutter/lib/ui/ui_dart_state.cc(186)] 未处理的异常:错误状态:无法获取 DocumentSnapshotPlatform 上不存在的字段

和我的

print (instance.collection('users').doc(userId).get());

返回这个:

I/flutter (25671):闭包:(字符串)=> 未来

您可以在此处查看我的代码(用于变量和其他):

【问题讨论】:

    标签: flutter dart google-cloud-firestore google-signin


    【解决方案1】:

    这里

    print(await instance.collection('users').doc(userId).get())
    

    您正在使用 Future 作为同步调用。您必须 await 获取值,然后如果文档不为空,请在其上使用 get。顺便说一句,在这种情况下,使用 ?. 运算符来保证空值安全,比如

    print(await instance.collection('users')?.doc(userId)?.get())
    

    【讨论】:

    • 您好 Simon Sot,我修改了我的代码:在我的回答中,但我的“运行”返回:[ERROR:flutter/lib/ui/ui_dart_state.cc(186)] 未处理的异常:错误状态:不能获取 DocumentSnapshotPlatform 上不存在的字段
    • 尝试把await放在instance.collection('users')?.doc(userId)?.get()里面fetUser函数之前
    • Simon Sot,我添加了等待和异步,但我的“运行”返回相同的错误...感谢您的帮助,我已经修改了我的答案。
    • getUser(String userId),但是你调用这个函数的时候有没有通过userId?使用getUser(response.user.uid)
    • 是的,我认为问题来自“userId”,我必须在 firestore 中获取 id
    【解决方案2】:

    除了this,要检查用户是否存在,您可以像这样使用 FirebaseAuth:

    void _checkIfExsist(String email) async {
        // final _auth = FirebaseAuth.instance;
        final _list = await _auth.fetchSignInMethodsForEmail(email);
        if(_list.isEmpty){
          //User not registered
        } else {
          //User registered
        }
      }
    

    编辑

    正如之前其他用户所说,您需要等待Future:

    void registergoogleUser() async {
        await _userProvider
            .registergoogleUser( _scaffoldKey)
            .then((response) async {
              if (response is Success<UserCredential>) {
                // You need to await the Futures!
                Future<DocumentSnapshot> getUser(String userId) async {
                  final FirebaseFirestore instance = FirebaseFirestore.instance;
                  print (instance.collection('users')?.doc(userId)?.get()); //print for test
                  return instance.collection('users')?.doc(userId)?.get();
                }
                final result = await getUser(userId);
                if (result != null) {
                  Navigator.of(context).pushNamedAndRemoveUntil(
                      TopNavigationScreen.id, (route) => false);
                } else {
                  Navigator.pop(context);
                  Navigator.pushNamed(context, RegisterScreen.id);
                }
              }
            }
          );
      }
    

    【讨论】:

    • 感谢您的回复,嗯“最终结果 = await getUser(userId);”返回错误:未定义的名称'userId'。这是一个要遵循的线索,我想我的代码一定有错误但我找不到它,或者,这就是我放我的github链接的原因,再次感谢
    猜你喜欢
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 2020-10-21
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    • 2019-01-05
    • 1970-01-01
    相关资源
    最近更新 更多