英文:
'Duplicate GlobalKey detected in widget tree' while deleting firebase documents in flutter
问题
在 Flutter 中删除 Firebase 文档时出现“widget 树中检测到重复 GlobalKey”错误。 我猜是因为字段中存在重复的值。我不确定,也不清楚。
我的应用程序运行良好,也能够在删除唯一值时删除文档,问题在于当发现重复值时,我的应用程序崩溃。
我的代码是...
final _firestore = Firestore.instance;
void choiceAction(String choice) {
if (choice == dConstants.Settings) {
print('Settings');
}
if (choice == dConstants.delete) {
_firestore.collection('orders').getDocuments().then((snapshot) {
for (DocumentSnapshot ds in snapshot.documents) {
ds.reference.delete();
}
});
}
}
错误信息如下...
═════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
在完成 widget 树时引发了以下断言:
在 widget 树中检测到重复的 GlobalKey。
在 widget 树中多次指定了以下 GlobalKey。这将导致 widget 树的部分意外截断,因为第二次看到键时,将先前的实例移动到新位置。这些键是:
- [LabeledGlobalKey<_OverlayEntryState>#bdcb5]
[LabeledGlobalKey<_OverlayEntryState>#902b8]
通过注意到在将具有上述全局键的小部件从其先前的父项移出之后,该先前的父项在此帧中从未更新,意味着它根本没有更新,或者在小部件被移动之前已经更新,无论哪种情况都意味着它仍然认为应该具有具有这些全局键的子项。
在由于 GlobalKey 重新父化而强制删除一个或多个子项后未更新的特定父项是:
- Stack(alignment: AlignmentDirectional.topStart, fit: expand, overflow: clip, dependencies: [Directionality], renderObject: RenderStack#9165c NEEDS-LAYOUT NEEDS-COMPOSITING-BITS-UPDATE DETACHED)
在 widget 树中一次只能在一个小部件上指定一个 GlobalKey。
抛出异常时,这是堆栈:
#0 BuildOwner.finalizeTree.<anonymous closure> (package:flutter/src/widgets/framework.dart:2578:15)
#1 BuildOwner.finalizeTree (package:flutter/src/widgets/framework.dart:2603:8)
#2 WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:775:18)
#3 RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:283:5)
#4 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1102:15)
...
═════════════════════════════════════════════════════════════════════════════════════════════════════
D/FlutterNativeView(10759): FlutterView.send called on a detached view, channel=plugins.flutter.io/cloud_firestore
I/chatty (10759): uid=10278(com.example.diet_manager) identical 18 lines
D/FlutterNativeView(10759): FlutterView.send called on a detached view, channel=plugins.flutter.io/cloud_firestore
D/FlutterNativeView(10759): FlutterView.send called on a detached view, channel=plugins.flutter.io/cloud_firestore
I/chatty (10759): uid=10278(com.example.diet_manager) identical 8 lines
D/FlutterNativeView(10759): FlutterView.send called on a detached view, channel=plugins.flutter.io/cloud_firestore
英文:
'Duplicate GlobalKey detected in widget tree' while deleting firebase documents in flutter.i'm guessing because i'm having duplicate values in the fields. i'm not sure and also i don't know.
my app is working fine and also it is deleting the documents,if deleting unique values..the problem is my app crashes when it finds the duplicate value.
my code is...
final _firestore=Firestore.instance;
void choiceAction(String choice){
if(choice == dConstants.Settings){
print('Settings');
}
if(choice == dConstants.delete){
_firestore.collection('orders').getDocuments().then((snapshot) {
for (DocumentSnapshot ds in snapshot.documents) {
ds.reference.delete();
}
});
}
}
and the error goes like this..
═══════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following assertion was thrown while finalizing the widget tree:
Duplicate GlobalKeys detected in widget tree.
The following GlobalKeys were specified multiple times in the widget tree. This will lead to parts of the widget tree being truncated unexpectedly, because the second time a key is seen, the previous instance is moved to the new location. The keys were:
- [LabeledGlobalKey<_OverlayEntryState>#bdcb5]
[LabeledGlobalKey<_OverlayEntryState>#902b8]
This was determined by noticing that after widgets with the above global keys were moved out of their previous parent, that previous parent never updated during this frame, meaning that it either did not update at all or updated before the widgets were moved, in either case implying that it still thinks that it should have a child with those global keys.
The specific parent that did not update after having one or more children forcibly removed due to GlobalKey reparenting is:
- Stack(alignment: AlignmentDirectional.topStart, fit: expand, overflow: clip, dependencies: [Directionality], renderObject: RenderStack#9165c NEEDS-LAYOUT NEEDS-COMPOSITING-BITS-UPDATE DETACHED)
A GlobalKey can only be specified on one widget at a time in the widget tree.
When the exception was thrown, this was the stack:
#0 BuildOwner.finalizeTree.<anonymous closure> (package:flutter/src/widgets/framework.dart:2578:15)
#1 BuildOwner.finalizeTree (package:flutter/src/widgets/framework.dart:2603:8)
#2 WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:775:18)
#3 RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:283:5)
#4 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1102:15)
...
════════════════════════════════════════════════════════════════════════════════════════════════════
D/FlutterNativeView(10759): FlutterView.send called on a detached view, channel=plugins.flutter.io/cloud_firestore
I/chatty (10759): uid=10278(com.example.diet_manager) identical 18 lines
D/FlutterNativeView(10759): FlutterView.send called on a detached view, channel=plugins.flutter.io/cloud_firestore
D/FlutterNativeView(10759): FlutterView.send called on a detached view, channel=plugins.flutter.io/cloud_firestore
I/chatty (10759): uid=10278(com.example.diet_manager) identical 8 lines
D/FlutterNativeView(10759): FlutterView.send called on a detached view, channel=plugins.flutter.io/cloud_firestore
专注分享java语言的经验与见解,让所有开发者获益!
评论