3

I'm trying to display an image from a svg String to the screen.

I'm using: https://pub.dartlang.org/packages/flutter_svg for svg support.

final String svgAsString = '''<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" width="109" height="109" viewBox="0 
0 109 109">
<g id="kvg:StrokePaths_080a1" style="fill:none;stroke:#000000;stroke- 
width:3;stroke-linecap:round;stroke-linejoin:round;">
<path id="kvg:080a1-s1" 
d="M20.22,18.74c0.77,0.77,0.79,2.14,0.8,3.05C21.38,62,20.62,75,11.5,89.39"/>
</g>
</svg>''';

I'm having trouble getting this rendered to either an image widget or draw onto a canvas. I've tried both ways and not getting anywhere.

The full code I will paste below: I'm not sure if I'm on the right tracks.

import 'dart:async';
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';

 class KanjiGradeGenerator extends StatefulWidget {
     @override
     _KanjiGradeState createState() => _KanjiGradeState();
 }

 class _KanjiGradeState extends State<KanjiGradeGenerator> {

 _KanjiGradeState() {}

displaySVG() async {

<g id="kvg:StrokePaths_080a1" style="fill:none;stroke:#000000;stroke- 
width:3;stroke-linecap:round;stroke-linejoin:round;">
<path id="kvg:080a1-s1" 
d="M20.22,18.74c0.77,0.77,0.79,2.14,0.8,3.05C21.38,62,20.62,75,11.5,89.39"/>
</g>
</svg>''';

final DrawableRoot svgRoot = await svg.fromSvgString(rawSvg, rawSvg);

final Picture picture = svgRoot.toPicture();

PictureRecorder picRec;
Canvas can = Canvas(picRec);

setState(() {
  can.drawPicture(picture);
});
}

Widget build(BuildContext context) {
    return new Scaffold(
      body: Container(
        child: displaySVG()
      ),
    );
 }
}

The error I receive is:

I/flutter ( 7791): type 'Future' is not a subtype of type 'Widget'

Peter O.
  • 30,765
  • 14
  • 76
  • 91
Jellyfish
  • 33
  • 1
  • 4

2 Answers2

2

Despite the documentation suggesting to do this:

final DrawableRoot svgRoot = await svg.fromSvgString(rawSvg, rawSvg);

I found you can run in to difficulties converting this to a Widget.

If your image string is encoded/decoded from Base64 then you can do this.

Once you have a string you can do:

import 'package:flutter_svg/flutter_svg.dart';

String decoded; // Decoded image

@override
Widget build(BuildContext context) {
    return new Scaffold(
      body: Container(
        child: SvgPicture.string(decoded)
      ),
    );
 }
0

You can use FutureBuilder widget for display image.

class KanjiGradeGenerator extends StatefulWidget {
  @override
  _KanjiGradeGeneratorState createState() => _KanjiGradeGeneratorState();
}

class _KanjiGradeGeneratorState extends State<KanjiGradeGenerator> {
  final rawSvg = 'YOUR_PATH';

  displaySvg() async {
    final DrawableRoot svgRoot = await svg.fromSvgString(rawSvg, rawSvg);
    return await svgRoot.toPicture().toImage(500, 500);
  }
  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: displaySvg(),
      builder: (context,snapshot){
        return Container(
          child: snapshot.data,
        );
      },
    );
  }
}
Saman
  • 1,986
  • 4
  • 20
  • 40