'flutter test not finding widget
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 ?
Solution 1:[1]
The test may actually work as intended: your second child widget in the Exanded my not be visible on screen, and can therefore not be found. The find methods will only return Widgets that have been created, and the Gridview will not create all widgets if they are not visible. The fact that it does show up if you swap the order suggests that the first Interno widget is large.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Bram |
