You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Firestore数据拉取与用户身份判断跳转问题求助

解决Firestore获取用户身份并跳转页面的问题

Hey there! Let's fix your issue step by step. I noticed a few critical mistakes in your modified code that are preventing the Firestore data fetch and redirect from working correctly.

主要问题分析

  • 字符串比较错误:Java中不能用==来比较字符串内容,它只会对比对象的引用地址,而不是实际文本。你需要用.equals()来做内容比较。
  • 数据库引用作用域问题:你在onCreate里声明的局部变量db无法在嵌套的onComplete回调中访问,应该使用你已经定义的全局变量fStore。
  • 缺失错误处理与UI状态重置:当前代码没有处理Firestore读取失败的情况,而且进度条会一直显示,影响用户体验。
  • 空值安全问题:如果Firestore中没有找到Student?字段,getString()会返回null,直接使用会引发空指针异常。

修正后的完整代码片段

替换你原来的signInWithEmailAndPassword的onComplete方法:

fAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
    @Override
    public void onComplete(@NonNull Task<AuthResult> task) {
        if (task.isSuccessful()) {
            FirebaseUser fUser = fAuth.getCurrentUser();
            userID = fAuth.getCurrentUser().getUid();
            
            if (userID != null) {
                // 使用全局的fStore引用,而不是局部的db
                DocumentReference docRef = fStore.collection("users").document(userID);
                docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                        progressBar.setVisibility(View.GONE); // 无论成功失败都隐藏进度条
                        if (task.isSuccessful()) {
                            DocumentSnapshot document = task.getResult();
                            if (document.exists()) {
                                String student = document.getString("Student?");
                                // 先判断student是否为空,再做比较,避免空指针
                                if (student != null) {
                                    Toast.makeText(Login_TA.this, student, Toast.LENGTH_SHORT).show();
                                    // 使用equals比较字符串内容
                                    if ("0".equals(student)) {
                                        startActivity(new Intent(getApplicationContext(), AssignmentTAActivity.class));
                                    } else {
                                        startActivity(new Intent(getApplicationContext(), SettingsActivity.class));
                                    }
                                    finish(); // 关闭当前登录页面,避免返回
                                } else {
                                    Toast.makeText(Login_TA.this, "用户身份字段不存在", Toast.LENGTH_SHORT).show();
                                }
                            } else {
                                Toast.makeText(Login_TA.this, "未找到用户数据", Toast.LENGTH_SHORT).show();
                            }
                        } else {
                            // 处理Firestore读取失败的情况
                            Toast.makeText(Login_TA.this, "读取用户数据失败: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        } else {
            Toast.makeText(Login_TA.this, "登录失败! " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
            progressBar.setVisibility(View.GONE);
        }
    }
});

额外注意事项

  • 确认Firestore数据与字段名:去Firebase控制台检查users集合下的对应文档,确保字段名是Student?(注意大小写和问号,Firestore字段名区分大小写),且字段值是"0"或"1"这样的字符串。
  • Firestore安全规则:确保当前登录用户有读取users集合的权限,测试阶段可以暂时使用宽松规则(上线前一定要修改为严格规则):
rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{userId} {
      allow read, write: if request.auth != null && request.auth.uid == userId;
    }
  }
}

内容的提问来源于stack exchange,提问作者Sidharth Nayyar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 21:02:56