Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render SVG in Flutter Web?

My final goal is to convert Flutter mobile app to Flutter web app. I used flutter_svg in Flutter mobile app but not working in Flutter web app.

What is the alternative plugin for Flutter web?

Here is error when I run project in chrome by using flutter run -d chrome.

The following UnimplementedError was thrown during paint():
UnimplementedError

The relevant error-causing widget was:
  RawPicture
  file:///C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/flutter_svg-0.17.4/lib/svg.dart:729:22

When the exception was thrown, this was the stack:
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 214:49  throw_
C:/b/s/w/ir/cache/builder/src/out/host_debug/flutter_web_sdk/lib/ui/src/ui/canvas.dart 898:5                               drawPicture
packages/flutter_svg/src/render_picture.dart 193:12                                                                        paint
packages/flutter/src/rendering/object.dart 2264:7                                                                          [_paintWithContext]
packages/flutter/src/rendering/object.dart 184:12                                                                          paintChild
packages/flutter/src/rendering/proxy_box.dart 131:14                                                                       paint

or How to fix this issue in flutter_svg plugin in Flutter Web?

like image 781
Diamond Avatar asked Jul 17 '20 10:07

Diamond


People also ask

How do I use SVG in Flutter web?

Honestly the best solution I've found is to load the image over a network for the web. Store the asset on a CDN somewhere, get it from the CDN on the web version, and retrieve it from your assets locally on iOS/Android. On the web in debug mode, I get the following error. So I don't think Image.network supports SVG.


1 Answers

For now you can use Image.network for web to render svg like this

child: kIsWeb 
    ? Image.network("/assets/$assetName",
    width: width,
    height: height,
    fit: fit,
    color: color,
    alignment: alignment,
    semanticLabel: semanticsLabel)
    : SvgPicture.asset(assetName,
    width: width,
    height: height,
    fit: fit,
    color: color,
    alignment: alignment,
    semanticsLabel: semanticsLabel)
like image 140
Godslave Avatar answered Oct 20 '22 02:10

Godslave