Skip to content

Commit

Permalink
Fixes issue where a incorrect key was being given to the ImageCache (#…
Browse files Browse the repository at this point in the history
…1000)

* Fixed the hashcode in SvgStringLoader and SvgBytesLoader.

It was accidently using the svg global object, instead of a local field. This caused caches (such as PaintingBinding.instance.imageCache) to misbehave.

* Add a test which ensures the embedded image in a SvgPicture is not incorrectly cached and carried over.
  • Loading branch information
bramp authored Oct 17, 2023
1 parent 6164f36 commit 16c062b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/flutter_svg/lib/src/loaders.dart
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ class SvgStringLoader extends SvgLoader<void> {
}

@override
int get hashCode => Object.hash(svg, theme, colorMapper);
int get hashCode => Object.hash(_svg, theme, colorMapper);

@override
bool operator ==(Object other) {
Expand Down Expand Up @@ -266,7 +266,7 @@ class SvgBytesLoader extends SvgLoader<void> {
String provideSvg(void message) => utf8.decode(bytes, allowMalformed: true);

@override
int get hashCode => Object.hash(svg, theme, colorMapper);
int get hashCode => Object.hash(bytes, theme, colorMapper);

@override
bool operator ==(Object other) {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 48 additions & 0 deletions packages/flutter_svg/test/widget_svg_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,54 @@ void main() {
matchesGoldenFile('golden_widget/two_of_same.png'),
);
});

// This tests https://github.com/dnfield/flutter_svg/issues/990
// Where embedded images were being incorrectly cached.
testWidgets('SvgPicture - with cached images', (WidgetTester tester) async {
// Simple red and blue 10x10 squares.
// Borrowed from https://gist.github.com/ondrek/7413434?permalink_comment_id=4674255#gistcomment-4674255
final Map<String, String> images = <String, String>{
'red':
'iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAFUlEQVR42mP8z8BQz0AEYBxVSF+FABJADveWkH6oAAAAAElFTkSuQmCC',
'blue':
'iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAFUlEQVR42mNkYPhfz0AEYBxVSF+FAP5FDvcfRYWgAAAAAElFTkSuQmCC',
};

// We keep pumping widgets into the same tester, to ensure the same cache
// is used on each iteration.
for (final String key in images.keys) {
final String image = images[key]!;
final String svgStr = '''<svg
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink" width="100" height="100">
<image width="100" height="100" href="data:image/png;base64,$image" />
</svg>''';

// First try with SvgPicture.string
await tester.pumpWidget(RepaintBoundary(
child: SvgPicture.string(svgStr),
));
await tester.runAsync(() => vg.waitForPendingDecodes());
await tester.pumpAndSettle();

Finder widgetFinder = find.byType(SvgPicture);
expect(widgetFinder, findsOneWidget);
await expectLater(
widgetFinder, matchesGoldenFile('golden_widget/image_$key.png'));

// Then with SvgPicture.memory
await tester.pumpWidget(RepaintBoundary(
child: SvgPicture.memory(utf8.encode(svgStr) as Uint8List),
));
await tester.runAsync(() => vg.waitForPendingDecodes());
await tester.pumpAndSettle();

widgetFinder = find.byType(SvgPicture);
expect(widgetFinder, findsOneWidget);
await expectLater(
widgetFinder, matchesGoldenFile('golden_widget/image_$key.png'));
}
});
}

class FakeAssetBundle extends Fake implements AssetBundle {
Expand Down

0 comments on commit 16c062b

Please sign in to comment.