【问题标题】:Why Firebase database (Firestore) does not add documents为什么 Firebase 数据库(Firestore)不添加文档
【发布时间】:2020-04-28 01:56:06
【问题描述】:

我正在尝试用这个添加 usersData:

import 'package:cloud_firestore/cloud_firestore.dart';

class DatabaseService {

    final String uid;
    DatabaseService({this.uid});

    // collection reference
    final CollectionReference nextCollection = Firestore.instance.collection('usersData');

    Future updateUserData(String firstName, String lastName, String country) async{

        return await nextCollection.document(uid).setData({
          'firstName':firstName,
          'lastName':lastName,
          'country':country,
        });
    }
}

在用户注册时调用它:

Future register(String email, String pwd) async {

    try {
      AuthResult result = await _auth.createUserWithEmailAndPassword(email: email, password: pwd);
      FirebaseUser user = result.user;

      //Create new document for the user 
      await DatabaseService(uid: user.uid)
      .updateUserData('Saad','AMRANI','laayoune');

      return _userFromFireBaseUser(user);
    } 
    catch (e) 
    {
      print(e.toString());
      return null;
    }
}

用户注册成功,但是usersData集合上以uid为document uniq key的初始数据不起作用!

日志截图

pubspec.yaml

dependencies:
   firebase_auth: ^0.14.0+5 
   cloud_firestore: ^0.12.9+4 

应用构建.gradle

 defaultConfig {
    // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
    applicationId "com.nextconcept.nextMain"
    minSdkVersion 16 //21
    multiDexEnabled true //added
    targetSdkVersion 28
    versionCode flutterVersionCode.toInteger()
    versionName flutterVersionName
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    //implementation 'com.google.firebase:firebase-analytics:17.2.0'
}

root build.gradle

 dependencies {
    classpath 'com.google.gms:google-services:4.0.1'
    classpath 'com.android.tools.build:gradle:3.5.0'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}

【问题讨论】:

  • 发布到 Stack Overflow 时,请不要使用文本截图。相反,将文本复制到问题本身中,以便于阅读和搜索。我们应该能够看到所有相关信息,而无需点击其他页面。
  • 感谢您的信息。

标签: firebase flutter dart async-await google-cloud-firestore


【解决方案1】:

您没有此集合的写入权限。

请参阅 this link 以管理 Cloud Firestore 安全规则。

允许任何登录用户读写:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth.uid != null;
    }
  }
}

【讨论】:

  • 谢谢它的工作。
猜你喜欢
  • 2020-01-04
  • 2018-09-24
  • 1970-01-01
  • 2019-02-02
  • 2021-04-08
  • 1970-01-01
  • 2019-09-04
  • 2018-07-15
  • 1970-01-01
相关资源
最近更新 更多