更新Firebase Flutter中的字段时出错

时间:2019-12-06 08:01:09

标签: firebase flutter

我创建了一个名为db的程序包,其中有一个名为category的dart文件。当用户想要更新数据库中的字段时,在主屏幕中有一个更新选项,该更新函数被调用从数据库包中获取。更新部分的代码如下所示:

class CategoryService{
  Firestore _firestore = Firestore.instance;

  void createCategory(String name){
  var id = Uuid();
  String categoryId = id.v1();

    _firestore.collection('categories').document(categoryId).upData({'category': name});
  }
}

如果我运行此代码并尝试在该字段中添加一个值,则会收到此错误。有人可以帮我解决这个问题。

W/DynamiteModule( 9396): Local module descriptor class for providerinstaller not found.
I/DynamiteModule( 9396): Considering local module providerinstaller:0 and remote module providerinstaller:0
W/ProviderInstaller( 9396): Failed to load providerinstaller module: No acceptable module found. Local version is 0 and remote version is 0.
I/dminbookingpag( 9396): The ClassLoaderContext is a special shared library.
I/dminbookingpag( 9396): The ClassLoaderContext is a special shared library.
I/dminbookingpag( 9396): The ClassLoaderContext is a special shared library.
W/dminbookingpag( 9396): Accessing hidden field Ljava/nio/Buffer;->address:J (light greylist, reflection)
V/NativeCrypto( 9396): Registering com/google/android/gms/org/conscrypt/NativeCrypto's 286 native methods...
W/dminbookingpag( 9396): Accessing hidden method Ljava/security/spec/ECParameterSpec;->getCurveName()Ljava/lang/String; (light greylist, reflection)
D/NetworkSecurityConfig( 9396): No Network Security Config specified, using platform default
I/ProviderInstaller( 9396): Installed default security provider GmsCore_OpenSSL
W/dminbookingpag( 9396): Accessing hidden field Ljava/net/Socket;->impl:Ljava/net/SocketImpl; (light greylist, reflection)
W/dminbookingpag( 9396): Accessing hidden method Ldalvik/system/CloseGuard;->get()Ldalvik/system/CloseGuard; (light greylist, linking)
W/dminbookingpag( 9396): Accessing hidden method Ldalvik/system/CloseGuard;->open(Ljava/lang/String;)V (light greylist, linking)
W/dminbookingpag( 9396): Accessing hidden field Ljava/io/FileDescriptor;->descriptor:I (light greylist, JNI)
W/dminbookingpag( 9396): Accessing hidden method Ljava/security/spec/ECParameterSpec;->setCurveName(Ljava/lang/String;)V (light greylist, reflection)
W/dminbookingpag( 9396): Accessing hidden method Ldalvik/system/BlockGuard;->getThreadPolicy()Ldalvik/system/BlockGuard$Policy; (light greylist, linking)
W/dminbookingpag( 9396): Accessing hidden method Ldalvik/system/BlockGuard$Policy;->onNetwork()V (light greylist, linking)
E/flutter ( 9396): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: PlatformException(Error performing updateData, NOT_FOUND: No document to update: projects/fire-7f723/databases/(default)/documents/categories/54f66f70-17fe-11ea-fdec-1965ecdfb96a, null)
E/flutter ( 9396): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:569:7)
E/flutter ( 9396): #1      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:316:33)
E/flutter ( 9396): <asynchronous suspension>
E/flutter ( 9396): #2      DocumentReference.updateData (package:cloud_firestore/src/document_reference.dart:69:30)
E/flutter ( 9396): #3      CategoryService.createCategory (package:adminbookingpage/db/promocode.dart:21:62)
E/flutter ( 9396): #4      _AdminState._categoryAlert.<anonymous closure> (package:adminbookingpage/screens/dashboard.dart:215:31)
E/flutter ( 9396): #5      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:654:14)
E/flutter ( 9396): #6      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:729:32)
E/flutter ( 9396): #7      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
E/flutter ( 9396): #8      TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:365:11)
E/flutter ( 9396): #9      TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:275:7)
E/flutter ( 9396): #10     PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:455:9)
E/flutter ( 9396): #11     PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:75:13)
E/flutter ( 9396): #12     PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:102:11)
E/flutter ( 9396): #13     GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:218:19)
E/flutter ( 9396): #14     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22)
E/flutter ( 9396): #15     GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7)
E/flutter ( 9396): #16     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7)
E/flutter ( 9396): #17     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7)
E/flutter ( 9396): #18     _rootRunUnary (dart:async/zone.dart:1136:13)
E/flutter ( 9396): #19     _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter ( 9396): #20     _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
E/flutter ( 9396): #21     _invoke1 (dart:ui/hooks.dart:263:10)
E/flutter ( 9396): #22     _dispatchPointerDataPacket (dart:ui/hooks.dart:172:5)
E/flutter ( 9396): 

1 个答案:

答案 0 :(得分:0)

您的错误是

Error performing updateData, NOT_FOUND: No document to update

从代码的外观来看,这是有道理的。 您正在尝试更新不存在的文档。而是创建该文档(并确保您具有写权限)。

我自己项目中的此代码段执行了类似的操作,在这里您还获得了分配的自动ID。

DocumentReference newItem = await Firestore.instance
    .collection('users')
    .document(myUserId)
    .collection('item')
    .add({
  "text": null,
  "date": Timestamp.now()
});