I have this code that test if the button is on the screen:
void main() {
testWidgets('Search for button', (WidgetTester tester) async {
await tester.pumpWidget(MaterialApp(
home: HomeView(),
));
final button = find.byType(BotaoRedirecionamentoInterno);
expect(button, findsWidgets);
});
}
Is working as I expected, but when I try this code that search for another type of button in the same view it doenst work:
void main() {
testWidgets('Search for button', (WidgetTester tester) async {
await tester.pumpWidget(MaterialApp(
home: HomeView(),
));
final button = find.byType(BotaoRedirecionamentoExterno);
expect(button, findsWidgets);
});
}
I don't understand why because they are exactly one line below in the HomeView:
Expanded(
child: GridView.count(
childAspectRatio: 6,
padding: EdgeInsets.only(left: screenWidth * .05, right: screenWidth * .05),
mainAxisSpacing: screenHeight * 0.02,
crossAxisCount: 1,
children: const [
BotaoRedirecionamentoInterno(texto: "aaa", rota: '/bbbb'),
BotaoRedirecionamentoExterno(texto: 'aaa', url: "bbb"),
BotaoRedirecionamentoExterno(texto: "aaa", url: "bbb"),
BotaoRedirecionamentoExterno(texto: "aaa", url: "bbb"),
BotaoRedirecionamentoExterno(texto: "aaa", url: 'bbb'),
BotaoRedirecionamentoExterno(texto: "aaa", url: "bbb"),
],
),
)
this is the error message :
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following TestFailure object was thrown running a test:
Expected: at least one matching node in the widget tree
Actual: _WidgetTypeFinder:<zero widgets with type "BotaoRedirecionamentoExterno" (ignoring
offstage widgets)>
I discovered that if I change the position inside the GridView to this:
Expanded(
child: GridView.count(
childAspectRatio: 6,
padding: EdgeInsets.only(left: screenWidth * .05, right: screenWidth * .05),
mainAxisSpacing: screenHeight * 0.02,
crossAxisCount: 1,
children: const [
BotaoRedirecionamentoExterno(texto: 'aaa', url: "bbb"),
BotaoRedirecionamentoInterno(texto: "aaa", rota: '/bbbb'),
BotaoRedirecionamentoExterno(texto: "aaa", url: "bbb"),
BotaoRedirecionamentoExterno(texto: "aaa", url: "bbb"),
BotaoRedirecionamentoExterno(texto: "aaa", url: 'bbb'),
BotaoRedirecionamentoExterno(texto: "aaa", url: "bbb"),
],
),
)
The test finds the BotaoRedirecionamentoInterno but wont find BotaoRedirecionamentoExterno, so my guess is that is only finding the first widget inside the GridView.
Anyone could help me figure out what is happening ?