Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?
      expect(...)
    });

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

like image 687
Joel Broström Avatar asked Sep 08 '26 18:09

Joel Broström


1 Answers

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.

like image 176
Christopher Moore Avatar answered Sep 11 '26 23:09

Christopher Moore



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!