RadioListTile not found in test

I am having trouble with a test where I am trying to see if tapping a RadioListTile works. Simply looking the element byType with finder, doesn't find it even though the parent widget and the list tiles are displayed in the emulator. I really can't figure out what I am doing wrong.

The widget tree is as follows:

RadioQuestion 
   | QuestionText
   | RadioChoices
      | RadioListTile
class RadioChoices extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Consumer<RadioQuestionModel>(
        builder: (context, model, child) => Column(
              // debugPrint(model.choices) here shows data.
              children: model.choices
                  .map((e) => RadioListTile(
                        title: e,
                        value: model.choices.indexOf(e),
                        groupValue: model.selectedIndex,
                        onChanged: (value) => model.onChanged(value),
                      ))
                  .toList(),
            ));
  }
}
testWidgets('Displays choices', (WidgetTester tester) async {
    await tester.pumpWidget(MaterialApp(home: Scaffold(body: RadioQuestion(questions[0]))));

    var finder = find.byType(RadioChoices);
    expect(finder, findsOneWidget);

    finder = find.byType(RadioListTile);
    expect(finder,
        findsNWidgets(questions[0].possibleChoices.length)); // This fails, 0 widgets found.
  });
The following TestFailure object was thrown running a test:
  Expected: exactly 4 matching nodes in the widget tree
  Actual: _WidgetTypeFinder:<zero widgets with type "RadioListTile<dynamic>" (ignoring offstage
widgets)>
   Which: means none were found but some were expected

1 answer

  • answered 2020-11-20 11:35 mirage

    Converting the List to an array of RadioListTiles fixed it:

    children: <RadioListTile>[]
                    ..addAll(model.choices.map((e) => RadioListTile(
                          title: e,
                          value: model.choices.indexOf(e),
                          groupValue: model.selectedIndex,
                          onChanged: (value) => model.onChanged(value),
                        )))