0

The Background: I am intending to use Flutter's animation package to run a container transform effect between a material card and another page (a details page). To set this up, the cards are set into a list within a "search" page to which then the container is opened, the DetailsScreen is passed a data object (an object within objectList according to the card's index). See below code:

SEARCHSCREEN
...
child: ListView.builder(
              controller: _listController,
              itemCount: objectList.length,
              itemBuilder: (BuildContext context, int index) {
                return OpenContainer(
                  transitionDuration: Duration(milliseconds: 750),
                  closedBuilder: (_, openContainer) {
                    return AnimatedCard(
                      direction: AnimatedCardDirection.left,
                      initDelay: Duration(milliseconds: 0),
                      duration: Duration(milliseconds: 500),
                      curve: Curves.easeOutBack,
                      child: ObjectCard(objectList[index],
                          savedObjectList, openContainer),
                    );
                  },
                  openBuilder: (_, closeContainer) {
                    return DetailScreen(objectList[index], closeContainer);
                  },
                );
              },
            ),

DETAILSCREEN
class DetailScreen extends StatefulWidget {
  const DetailScreen(this.object, this.closeContainer);
  final object;
  final closeContainer;

  @override
  _DetailScreenState createState() => _DetailScreenState();
}

class _DetailScreenState extends State<DetailScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
...

The Problem: Whenever a card is clicked upon, the following error is returned:

The widget which was currently being built when the offending call was made was:
  DetailScreen-[LabeledGlobalKey<_SearchScreenState>#b16ed]
When the exception was thrown, this was the stack:
...
═════ Exception caught by animation library ═════════════════════════════════
setState() or markNeedsBuild() called during build.
════════════════════════════════════════════════════════════════════════════════
Another exception was thrown: setState() or markNeedsBuild() called during build.
Another exception was thrown: Duplicate GlobalKey detected in widget tree.

════════ Exception caught by widgets library ═══════════════════════════════════
Duplicate GlobalKey detected in widget tree.

Having tried many solutions of adding and passing keys first to the constructors of each object, then to all of them, nothing has worked. The only thing that makes the animation work is if I remove the [index] from "return DetailScreen(objectList[index], closeContainer);". The animation will fire, but obviously is not desirable as now no object has been passed and hence nothing on the DetailScreen will work. What am I missing here? I was under the impression that on each openContainer click, a DetailsScreen was created, populated with the passed object's data and the destroyed when I used the closeContainer callback. Under this assumption, this should work because only one screen will exist at a time and no duplication of "Global Keys" will occur. But this now does not seem to be the case. Is there something up that I need to include higher up the widget chain, such as within my "SearchScreen" itself, which is hosting this code? Any help or insight would be greatly appreciated!

J. Squillaro
  • 155
  • 2
  • 13
  • You code sample does not include any keys, global or otherwise. Where are you creating and using this `GlobalKey<_SearchScreenState>`? – Nitrodon Oct 19 '20 at 15:26
  • @Nitrodon I have no explicitly written keys into my code (there is no mention of a key in _SearchScreenState nor in its child widgets. I believe the " DetailScreen-[LabeledGlobalKey<_SearchScreenState>#b16ed]" in the error is a generic one created by default? Does a global key have to be explicitly stated and how would such an implementation occur? – J. Squillaro Oct 19 '20 at 15:53
  • I also see here https://stackoverflow.com/questions/51329065/builder-versus-globalkey that there are issues when using global key within a builder, which I am with listbuilder, and that it shouldn't be used in it of itself. Is this just something that this package is not compatible with? – J. Squillaro Oct 19 '20 at 16:41

0 Answers0