Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SvgPicture image rendering error in flutter

Tags:

image

svg

flutter

im using svgPicture to show the image and every time at first it showing error and then it showing image.

SvgPicture.asset(
 'assets/Images/otpLogo.svg',
  height: SizeConfig.blockSizeVertical * 26,                      
),

and the error is

══╡ EXCEPTION CAUGHT BY SVG ╞═══════════════════════════════════════════════════════════════════════ I/flutter (18256): The following assertion was thrown in _getDefinitionPaint: I/flutter (18256): Failed to find definition for url(#paint0_linear)
I/flutter (18256): This library only supports and xlink:href references that are defined ahead of their I/flutter (18256): references. I/flutter (18256): This error can be caused when the desired definition is defined after the element referring to it I/flutter (18256): (e.g. at the end of the file), or defined in another file. I/flutter (18256): This error is treated as non-fatal, but your SVG file will likely not render as intended

like image 236
lokesh paladugula Avatar asked Apr 14 '20 07:04

lokesh paladugula


People also ask

Why is SVG not showing in Flutter?

The Main Version of Flutter Does Not Support SVG There is an SVG directory in the code of Skia, which is a basic component of Flutter. However, Skia can only serialize images into SVG files. Therefore, you cannot decode or render SVG images with Skia.

What is SVG in flutter?

Scalable Vector Graphics (SVG) is one of the most widely used file image formats in applications. Because it offers several advantages over bitmap files, especially when it comes to retaining image quality while scaling, it’s difficult to start building a Flutter application without first considering using SVG.

Why is my SVG file not rendering as intended?

I/flutter (18256): This error is treated as non-fatal, but your SVG file will likely not render as intended Show activity on this post. This error can be caused when the desired definition is defined after the element referring to it...

Can Skia decode or render SVG images?

Skia, a 2D graphics library and core component of Flutter, can only serialize images into SVG files. Thus, decoding or rendering SVG images with Skia is not possible.

How to load an SVG from an SVG code?

Finally, you can load an SVG from an SVG code: Once you have your SVG file loaded, the first step is to change the color or tint of the image: Using a semantics label helps to describe the purpose of the image and enhances accessibility.


2 Answers

I'm not sure if this is the exact problem since you did not provide the code to the SVG file but according to the error message, it says:

This error can be caused when the desired definition is defined after the element referring to it...

For me, anyway, the solution was to edit the SVG file and move the <defs> tag and all its contents up to just below the opening of the <svg> tag.


You can improve and optimize your SVG code structure by using this online tool. Then simply cut and paste the defs as explained above.

Nonetheless, there is still an open issue in the repo about this particular problem.


Sample Case:

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="24" height="24" viewBox="0 0 24 24">
    <g fill="none" fill-rule="evenodd" transform="translate(8 6)">
        <mask id="prefix__b" fill="#fff">
            <use xlink:href="#prefix__a"/>
        </mask>
        <g fill="#FFF" mask="url(#prefix__b)">
            <path d="M0 0H24V24H0z" transform="translate(-8 -6)"/>
        </g>
    </g>
    <defs>
        <path id="prefix__a" d="M7.705 1.41L6.295 0 0.295 6 6.295 12 7.705 10.59 3.125 6z"/>
    </defs>
</svg>

Fixed version:

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="24" height="24" viewBox="0 0 24 24">
    <defs>
        <path id="prefix__a" d="M7.705 1.41L6.295 0 0.295 6 6.295 12 7.705 10.59 3.125 6z"/>
    </defs>
    <g fill="none" fill-rule="evenodd" transform="translate(8 6)">
        <mask id="prefix__b" fill="#fff">
            <use xlink:href="#prefix__a"/>
        </mask>
        <g fill="#FFF" mask="url(#prefix__b)">
            <path d="M0 0H24V24H0z" transform="translate(-8 -6)"/>
        </g>
    </g>
</svg>

Note: Notice the changing position of the defs tag.

like image 51
Giraldi Avatar answered Oct 10 '22 04:10

Giraldi


Before in my svg file:

<svg>
  <g>
     ...
  </g>
  <defs>
     ...
  </defs>
</svg>

After in my svg file:

<svg>
  <defs>
     ...
  </defs>
  <g>
     ...
  </g>
</svg>

Move <defs></defs> to up!

like image 15
trava Avatar answered Oct 10 '22 04:10

trava