Skip to content

[flutter_svg] Incorrectly loads SVG assets assuming each svg-image has a separate buffer underliing. #174526

@bshishov

Description

@bshishov

Affects third-party flutter_svg v2.2.0 plugin in flutter/packages.

Third-party package (moved to flutter/packages) flutter_svg in some elaborate use-cases incorrectly loads assets assuming that AssetBundle provides a distinct buffer per asset.

Context

I'm trying to implement an AssetBundle for web that preloads all the assets in a single file so i can reduce number of asset requests on web. My custom implementation of AssetBundle relies on a single byte buffer being loaded once. Then AssetBundle.load provides a view (slice) of that buffer, like asset X starts from byte Y and is of size Z.
All the basic flutter functionality, i.e. Images are nicely working with ByteData object returned from AssetBundle.load().

But not flutter_svg asset loader.

Problem

It does this (https://github.com/flutter/packages/blob/86fbeecb7b4f6760391a934a1d3d0e160d31b653/third_party/packages/flutter_svg/lib/src/loaders.dart#L381):

  String provideSvg(ByteData? message) =>
      utf8.decode(message!.buffer.asUint8List(), allowMalformed: true);

when it should be doing this:

  String provideSvg(ByteData? message) =>
      utf8.decode(Uint8List.sublistView(message!), allowMalformed: true);

like it is stated in the AssetBundle docs (

Future<ByteData> load(String key);
)

in the first example it converts an entire underlying buffer to uint8list when it should just take a portion specified in ByteData message.
This ruins my attempt with AssetBundle being a single buffer on web.

I'm proposing a PR myself.

Metadata

Metadata

Assignees

No one assigned

    Labels

    p: flutter_svgThe Flutter SVG drawing packagespackageflutter/packages repository. See also p: labels.team-engineOwned by Engine team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions