如何在 Flutter 中测试 Image widgets 源路径
How to test Image widgets source path in Flutter
我有以下设置:
return Container(
child: widget.searchResult.companyLogoUrl.isEmpty
? Image.asset('assets/images/missing-company-logo.png')
: Image.network(widget.searchResult.companyLogoUrl),
)
现在我想测试在没有提供 url 时加载丢失的徽标图像。
在测试中获取Image widget后如何获取源url或本地文件?
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(...)
});
有没有办法知道加载了哪张图片?
使用 Image
小部件 class 的 image
属性 检查图像提供者是什么。从那里您可以提取单个属性:
String source;
if(image.image is AssetImage) {
source = image.image.assetName;
} else if(image.image is NetworkImage) {
source = image.image.url;
}
您可以根据需要扩展他的以包括更多可能的图像提供者。
我有以下设置:
return Container(
child: widget.searchResult.companyLogoUrl.isEmpty
? Image.asset('assets/images/missing-company-logo.png')
: Image.network(widget.searchResult.companyLogoUrl),
)
现在我想测试在没有提供 url 时加载丢失的徽标图像。 在测试中获取Image widget后如何获取源url或本地文件?
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(...)
});
有没有办法知道加载了哪张图片?
使用 Image
小部件 class 的 image
属性 检查图像提供者是什么。从那里您可以提取单个属性:
String source;
if(image.image is AssetImage) {
source = image.image.assetName;
} else if(image.image is NetworkImage) {
source = image.image.url;
}
您可以根据需要扩展他的以包括更多可能的图像提供者。