Flutter 无法将request.auth.id与Firestore规则中的文档id匹配

Flutter 无法将request.auth.id与Firestore规则中的文档id匹配,flutter,google-cloud-firestore,firebase-security,Flutter,Google Cloud Firestore,Firebase Security,我正在开发一个应用程序与颤振。我在Firestore上有一个数据库,其结构为:Users/(UserID)/collection/document 这里UserID是创建(UserID)文档的用户的唯一uid。 该代码用于获取用户的uid user.uid; 其中user是FirebaseUser的实例。 我正在尝试设置规则,以便只有当request.auth.uid与(UserID)文档id匹配时,只有用户才能读/写 service cloud.firestore { match /da

我正在开发一个应用程序与颤振。我在Firestore上有一个数据库,其结构为:Users/(UserID)/collection/document 这里UserID是创建(UserID)文档的用户的唯一uid。 该代码用于获取用户的uid

user.uid;
其中user是FirebaseUser的实例。 我正在尝试设置规则,以便只有当request.auth.uid与(UserID)文档id匹配时,只有用户才能读/写

service cloud.firestore {
  match /databases/{database}/documents {
    match /Users/{documentID} {
      allow read, write: if isOwner(documentID);
    }

    function isOwner(documentID){
        return request.auth.uid == documentID;
    }
  }
}
但我在这件事上犯了错误

失败:状态{code=PERMISSION\u DENIED,description=缺少或权限不足,cause=null}

这是执行查询的代码

class EmployeeList extends StatefulWidget {
  static bool isMale;
  final String userId;
  EmployeeList([this.userId]);
  void setIsMale(bool gen) {
    isMale = gen;
  }

  bool get getIsMale => isMale;

  @override
  State<StatefulWidget> createState() => _EmployeeList();
}

class _EmployeeList extends State<EmployeeList> {
  String employeeName;

  final TextEditingController textEditingController = 
TextEditingController();
  String gender;
  int keyIndex;
  CollectionReference listColRef;
  bool firstTime;
  Firestore db = Firestore.instance;

  @override
  void initState() {
    gender = EmployeeList().getIsMale ? "Male" : "Female";
    listColRef = 
db.collection('Users').document(widget.userId).collection('EmployeeList');
    print(widget.userId);
    super.initState();
  }

 @override
 Widget build(BuildContext context) {
   return Scaffold(
      backgroundColor: Colors.grey[100],
      appBar: AppBar(
         title: Text('Employees'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(5),
        child: StreamBuilder<QuerySnapshot>(
            stream: listColRef.where('Gender', isEqualTo: 
        gender).snapshots(),
class EmployeeList扩展了StatefulWidget{
静态布尔伊斯梅尔;
最终字符串用户标识;
EmployeeList([this.userId]);
void setIsMale(布尔根){
isMale=gen;
}
bool get getIsMale=>isMale;
@凌驾
State createState()=>_EmployeeList();
}
类_EmployeeList扩展状态{
字符串employeeName;
最终文本编辑控制器文本编辑控制器=
TextEditingController();
字符串性别;
int键索引;
收集参考列表colref;
布尔第一次;
Firestore db=Firestore.instance;
@凌驾
void initState(){
性别=雇员列表().getIsMale?“男”:“女”;
listColRef=
db.collection('Users').document(widget.userId).collection('EmployeeList');
打印(widget.userId);
super.initState();
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
背景颜色:颜色。灰色[100],
appBar:appBar(
标题:文本(“员工”),
),
主体:填充物(
填充:常量边集。全部(5),
孩子:StreamBuilder(
流:listColRef.where('Gender',isEqualTo:
性别),

这是数据库的结构:

您的数据库规则与您的查询不匹配。您的查询正在尝试使用模式
/Users/{userId}/EmployeeList
查找集合下的文档。但您的规则仅允许访问
/Users/{documentID}下的文档
。如果要允许访问嵌套子集合中的文档,则需要确保整个路径匹配。例如:

match /Users/{userId}/EmployeeList/{id} {
  allow read, write: if isOwner(userId);
}

请编辑问题以显示执行查询的代码,该代码的工作方式与您预期的不符。您确定请求uid和您的文档id相同吗?默认情况下,身份验证用户和数据库用户不会使用firebase链接。除了Doug要求的代码外,我们可能还希望看到您尝试访问的文档阅读。一个屏幕截图通常是最好的。如果我链接Github repo会好吗?或者你希望我添加代码本身吗?这是数据库的结构。