7

If I use GlobalKey for some widget

final gridBuilder = GridView.builder(
      itemCount: movies.length,
      key: GlobalKey(debugLabel: "gridView"),

I can not access my GridView while testing by calling

find.byKey(Key('gridView'));

nether

find.byKey(GlobalKey(debugLabel: "gridView"));

Any solution except finding by type or text?

Vermillion828
  • 91
  • 1
  • 7

2 Answers2

10

I'm running into the same problem here, did you find a way to get around properly?

Didn't find any other way to preserve the GlobalKey on the widget than wrapping it in a Container and giving it a Key:

final Container gridBuilder = Container(
  key: Key('gridView'),
  child: GridView.builder(
    itemCount: movies.length,
    key: GlobalKey(),
  ),
);

Then in your test you can access the GridView this way:

find.descendant(of: find.byKey(Key('gridView')), matching: find.byType(GridView));
Jordan
  • 101
  • 1
  • 4
0

I had exactly the same issue. the @jordan LL solution as perfect!!! As a workaround, i've used tha follow strategy: Instead use: var key1 = find.byKey(ValueKey(key'$OVERVIEW_GRID_ITEM_FAVORITE_BUTTON_KEY\_0'));

I have workedaroung using: var key1 = find.descendant( of: find.byKey(Key('$OVERVIEW_GRID_ITEM_FAVORITE_BUTTON_KEY\_0')), matching: find.byType(OverviewGridItem));

GtdDev
  • 748
  • 6
  • 14