[FIXED] How to test Image widgets source path in Flutter


I have the following setup:

return Container(
      child: widget.searchResult.companyLogoUrl.isEmpty
          ? Image.asset('assets/images/missing-company-logo.png')
          : Image.network(widget.searchResult.companyLogoUrl),

And now I want to test that the missing logo image is loaded when no url is provided.
How can I get the source url or local file after obtain the Image widget in a test?

testWidgets('given', (tester) async {
      await tester.pumpWidget(createSearchResultCard(mockResponse));
      await tester.pumpAndSettle();
      final Image image = find.byType(Image).evaluate().single.widget as Image;
      final String source = image. // How do I get the file source?

Is there a way to tell which picture has been loaded?


Check what the image provider is with the image property of the Image widget class. From there you can extract individual properties:

String source;
if(image.image is AssetImage) {
  source = image.image.assetName;
} else if(image.image is NetworkImage) {
  source = image.image.url;

You could expand his to include more possible image providers as needed.

Answered By – Christopher Moore

Answer Checked By – Terry (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published