【问题标题】:Cloud Firestore not saving data flutterCloud Firestore 不保存数据颤动
【发布时间】:2021-05-06 08:32:18
【问题描述】:

我正在学习 Flutter,作为练习,我正在与 Firebase 交互(我也是新手——我的第一次接触)。 在练习中,当我们注册一个新用户时,我们会为新注册的用户创建一个具有默认值的文档。

一个新用户正在注册,但集合和文档都没有创建。

我创建的 Cloud Firestore 是测试模式。下面附上规则。

Rules :
rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if
          request.time < timestamp.date(2021, 3, 4);
    }
  }
}

auth.dart

//Resgister using email and password
  Future registerWithEmailPassword(
      {@required String email, @required String password}) async {
    try {
      Firebase.UserCredential resut = await _auth
          .createUserWithEmailAndPassword(email: email, password: password);
      Firebase.User user = resut.user;

      //create a new document for user with uid
      await DatabaseService(uid: user.uid).updateUserData(
        sugars: '0',
        name: 'new crew member',
        strength: 100,
      );
      return _userFromFirebase(user);
    } catch (e) {
      //print(e.toString());
      return e.toString();
    }
  }

database.dart

import 'package:cloud_firestore/cloud_firestore.dart';

class DatabaseService {   final String uid;   DatabaseService({this.uid});   
//colection reference   
final CollectionReference brewCollection =
      FirebaseFirestore.instance.collection('brews');


  Future updateUserData({String sugars, String name, int strength}) async {
    return await brewCollection
        .doc(uid)
        .set({'sugars': sugars, 'name': name, 'strength': strength});   } }
}

我加了一个断点,检查updateUserData中的数据,数据是正确的。

【问题讨论】:

  • 在你DatabaseService类中,brewCollection被注释掉了!
  • @ASADHAMEED,当我将代码粘贴到堆栈溢出的编辑器中时,这就是问题所在。我检查了代码 final CollectionReference brewCollection = FirebaseFirestore.instance.collection('brews');没有评论,问题正在形成。
  • 你想从updateUserData返回什么?
  • @RehmatSinghGill,目前我不希望返回任何东西。稍后我将尝试使用更新状态进行布尔运算。
  • 你的代码看起来不错; catch 块中是否有任何异常?

标签: flutter google-cloud-firestore


【解决方案1】:

他们是 Firebase 插件的问题。

MissingPluginException(No implementation found for method DocumentReference#set on channel plugins.flutter.io/firebase_firestore)

我通过pubspec.yaml 更新了最新版本的文件然后运行以下命令

flutter clean
flutter packages get

【讨论】:

    猜你喜欢
    • 2021-06-25
    • 2020-06-29
    • 2018-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-21
    • 1970-01-01
    相关资源
    最近更新 更多