flutter_svg
flutter_svg copied to clipboard
Using flutter_svg in tests
I'm using flutter_svg
throughout our project and it works just fine. However, if I'm running the app in an integration test, the app crashes on assets that cannot be found. The error message is as follows, I'm using the latest flutter_svg
version:
══╡ EXCEPTION CAUGHT BY SVG ╞═══════════════════════════════════════════════════════════════════════
The following assertion was thrown resolving a single-frame picture stream:
Unable to load asset: assets/buttons/close_button.svg
This is the code in my test:
import '../main.dart' as app;
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
group('end-to-end test', () {
testWidgets('tap on the floating action button, verify counter',
(tester) async {
app.main();
await tester.pumpAndSettle();
Any thoughts, remarks? Do I need to provide more information?
I had the same issue SvgPicture.network, and I solved with HttpOverrides.
In your case, you could mock the AssetBundle.
Another solution that I used in my code, is to convert the svg icons to a font pack. That way I can use them as Icon
like I would do with a material icon, instead of Svg.
Realise this was a long time ago, but what folders do you have added to your pubspec.yaml? I saw this issue when I forgot to add the path to the containing folder, as well as the overall folder. ie
assets:
- assets/
- assets/buttons/
How the assets loaded was actually not consistent between running the app and running the tests. Adding the subfolders made it work for both.