How can SVG images using href be displayed in a Flutter app with flutter_svg?

Displaying SVG images in a Flutter application can be challenging, especially when these images contain href attributes and the project utilizes flutter_svg. An example of the issue arises with SVG code such as , which seems to impede correct rendering. The need for a solution becomes more pressing considering that removing the href attribute adversely affects the app’s functionality.

Is there an effective method or workaround within flutter_svg to enable the proper display of SVG images with href attributes?

1 Like

@komal, while integrating SVG images into a Flutter app using the flutter_svg package, encountering hurdles, especially with SVG code containing href attributes, is commonplace. The glitch stems from flutter_svg not consistently managing these attributes, leading to inaccurate SVG rendering.

For instance,

<image xlink:href="image.png" /> poses a challenge due to its href attribute defining the image source.

Preserving href attributes in SVG files is vital for linking external resources like images or other SVG files. Stripping them could disrupt app functionality. Thus, it is crucial to identify a reliable workaround or solution within flutter_svg to ensure the correct SVG display with href attributes intact.

One workaround involves preprocessing SVG files before rendering in the Flutter app. This entails modifying the SVG code to substitute href attributes with data URI equivalents. For instance, replacing


<image xlink:href="image.png" /> with <image href="data:image/png;base64,..." />

embeds base64-encoded image data directly into the SVG file.

Alternatively, exploring alternative SVG rendering libraries or plugins for Flutter might offer better support for href attributes and external resource linking. These alternatives could handle SVG files with href attributes more robustly, obviating the need for preprocessing.

Ultimately, the approach should align with the app’s requirements and constraints. Evaluating trade-offs between solutions considering performance, maintainability, and compatibility is essential. Keeping abreast of flutter_svg package updates and community feedback may unveil improvements or new features addressing the issue more effectively.