nabil6391 / graphview

Flutter GraphView is used to display data in graph structures. It can display Tree layout, Directed and Layered graph. Useful for Family Tree, Hierarchy View.
MIT License
415 stars 115 forks source link

Same key is being used by multiple widgets in GraphView.dart #85

Open VikasJilla opened 2 years ago

VikasJilla commented 2 years ago

Hi,

I have observed that in GraphView.dart, when we pass a key to GraphView, you are passing it to both super constructor, as well as the widget being returned in build method. I think this should not be done. Because of this we are getting an issue as below:

The following assertion was thrown while finalizing the widget tree:
Multiple widgets used the same GlobalKey.
The key [LabeledGlobalKey<State<GraphView>>#f63ba] was used by multiple widgets. The parents of those widgets were:
- GraphView-[LabeledGlobalKey<State<GraphView>>#f63ba](state: _GraphViewState#22b82)
- KeyedSubtree-[GlobalKey#0985f]
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._debugVerifyGlobalKeyReservation.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:flutter/src/widgets/framework.dart:2868:13)
#1      _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:614:13)

Please resolve this as soon as possible. Please let me know if there is something I missed.

nabil6391 commented 1 year ago

Hi, By any chance do you haev any example code that I can look into. Like for example whats the keys you are using. You do have to ensure that there are no duplicate keys for the nodes

VikasJilla commented 1 year ago

@nabil6391 below is the sample code that was leading to the error.

final GlobalKey _graphViewKey = GlobalKey<State<GraphView>>();
 GraphView(
        key: _graphViewKey,
        graph: graph,
        algorithm: walkerAlgorithm!,
        animated: true,
        paint: Paint()
          ..color = AppColors.relationsGreyColor
          ..strokeWidth = 1
          ..style = PaintingStyle.stroke,
        builder: (Node node) {
          return buildNode(node);
        },
      )