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

[Flutter] Flutter : RenderRepaintBoundary lateinitializationerror on device

Discussão em 'Mobile' iniciado por Stack, Outubro 5, 2024 às 07:02.

  1. Stack

    Stack Membro Participativo

    Below code work on emulator, but on device it throws LateInitializationError : Local 'Result' has not been initialized. Kindly suggest solution.

    RepaintBoundry has Stack of two or more widgets with one Network Image.

    I've tried with Gal, ImageGallerySaver,GallerySaver flutter package, but same issue.

    GlobalKey globalKey = GlobalKey();

    @override
    void initState() {
    super.initState();
    }

    Future<Uint8List> _capturePng() async {
    RenderRepaintBoundary boundary =
    globalKey.currentContext!.findRenderObject() as RenderRepaintBoundary;

    if (boundary.debugNeedsPaint) {
    await Future.delayed(const Duration(milliseconds: 20));

    return _capturePng();
    }

    var image = await boundary.toImage();
    var byteData = await image.toByteData(format: ui.ImageByteFormat.png);

    int id = DateTime.now().millisecondsSinceEpoch;
    final imagePath = '${Directory.systemTemp.path}/$id.png';

    await Gal.requestAccess(toAlbum: true);
    await Gal.putImageBytes(byteData!.buffer.asUint8List(), name: imagePath);
    return byteData.buffer.asUint8List();
    }

    Widget build(BuildContext context) {
    return Scaffold(
    appBar: AppBar(
    title: const Text('Save Image to gallery'),
    ),
    body: Center(
    child: Column(
    children: [
    Padding(
    padding: const EdgeInsets.all(8.0),
    child: Text(message),
    ),
    RepaintBoundary(
    key: globalKey,
    child: Stack(
    children: [
    Image.network(
    url,
    width: 200,
    ),
    const Positioned(
    child: Text(
    'positioned text',
    style: TextStyle(color: Colors.white),
    ),
    bottom: 10,
    left: 20,
    )
    ],
    )),
    ElevatedButton(
    onPressed: () {
    saveImageToGallerytest();
    },
    child: const Text('Save Image'))
    ],
    ),
    ),
    );
    }```

    Continue reading...

Compartilhe esta Página