1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] How to parse SVG or catch parsing errors with flutter_svg

Discussão em 'Mobile' iniciado por Stack, Outubro 1, 2024 às 06:42.

  1. Stack

    Stack Membro Participativo

    I would like to parse SVG string to test if it is a valid SVG. Is this possible?

    I am struggling a little with error handling. Try/catch does not work and neither does RunZonedGuarded. To see what I mean try to load an invalid svg string:

    try {
    SvgPicture.string('not svg');
    }
    catch {
    //not catching
    }


    Below approach sort of catches the error without crashing the app, but not very gracefully. The unpleasant looking android error is displayed in the image box and it overflows.

    [​IMG]

    FutureBuilder<String>(
    future: Future.value(_shopLogoSvg),
    builder: (context, snapshot) {
    if (snapshot.connectionState == ConnectionState.done) {
    return SvgPicture.string(snapshot.data!,
    fit: BoxFit.contain,
    allowDrawingOutsideViewBox: false,);
    } else {
    return const Center(
    child: CircularProgressIndicator()); // or any other loading indicator
    }
    },
    );


    It would help to have SvgPicture.parse method that could be used to parse non-trusted SVG strings before loading.

    Or perhaps there is a better way to properly handle errors with flutter_svg?

    Any help appreciated!

    Continue reading...

Compartilhe esta Página