Add id to element in Flutter

Viewed 3971

The tester needs to have an id on flutter Elements. I am looking at the documentation and this is what I found

https://api.dart.dev/stable/2.10.2/dart-html/Element/id.html

But I can't figure how to do this. I try for example

Padding(
  id: "some id",
  // ...
)

And of course it doesn't work. Is there a way to add an id for the test automation?

Thank you

2 Answers

you need to add keys into your widgets, for example if I want to test when I use an API and render a card into a ListView builder, I can find the card with a key

This link is all you need: https://flutter.dev/docs/cookbook/testing/widget/finders

PD: you are looking for the wrong documentation, search the documentation of Flutter, no Dart

You need to use the key properties of the related widgets. You can look at this example.

   child: Text(
        'Go to next route',
        key: Key('next_route_key'),
      ),

Or for a FloatingActionButton:

   floatingActionButton: FloatingActionButton(
       // Provide a Key to this button. This allows finding this
       // specific button inside the test suite, and tapping it.
       key: Key('increment'),
       onPressed: () => plusClickSink.add(null),
       tooltip: 'Increment',
       child: Icon(Icons.add),
     ),       
Related