简体   繁体   English

共享和打印屏幕 Flutter

[英]Share and Print Screen Flutter

import 'dart:io';
import 'dart:typed_data';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:esys_flutter_share/esys_flutter_share.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

Imports...进口...

  screenshot: ^0.2.0
  esys_flutter_share: ^1.0.2

I only need to take a capture, and then share it I am using the following code but I receive the error:我只需要捕获,然后分享它我正在使用以下代码,但我收到错误:

    _takeScreenshotandShare() async {
    _imageFile = null;
    screenshotController
        .capture(delay: Duration(milliseconds: 10), pixelRatio: 2.0)
        .then((File image) async {
      setState(() {
        _imageFile = image;
      });
      final directory = (await getApplicationDocumentsDirectory()).path;
      Uint8List pngBytes = _imageFile.readAsBytesSync();
      File imgFile = new File('$directory/screenshot.png');
      imgFile.writeAsBytes(pngBytes);
      print("File Saved to Gallery");
      await Share.file('Anupam', 'screenshot.png', pngBytes, 'image/png');
    }).catchError((onError) {
      print(onError);
    });
  }

My mistake is:我的错误是:

I/flutter ( 2486): NoSuchMethodError: The method 'findRenderObject' was called on null.
I/flutter ( 2486): Receiver: null
I/flutter ( 2486): Tried calling: findRenderObject()

I face the same issue with screenshot package so my workaround is call same function again incase of error occur.我在屏幕截图 package 上遇到了同样的问题,所以我的解决方法是再次调用相同的 function 以防发生错误。

screenshotController.capture().then((File image) async {
      Uint8List pngBytes = image.readAsBytesSync();
      final directory = (await getApplicationDocumentsDirectory()).path;
      File imgFile = new File('$directory/${DateTime.now().millisecondsSinceEpoch}.png');
      await imgFile.writeAsBytes(pngBytes);
      if(pngBytes.length == 0)
        // call Same function again
      else 
       // your image
    }).catchError((onError) {
      print(onError);
      Future.delayed(Duration(seconds: 1)).then((value) => //call Same function again);
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter 最佳实践在屏幕之间共享数据 - Flutter best practices share data between screen 在 flutter 的一个屏幕中的选项卡之间共享数据? - share data between tabs in one screen in flutter? Flutter - 使用 flutter 插件的图像共享给我一个黑屏? - Flutter - Image share using flutter plugin giving me a blackend screen? Flutter如何通过屏幕打印值? - Flutter how to print value pass through screen? Flutter中有什么方法可以让两个用户共享一个屏幕? - Is there any way in Flutter to let two users share one screen? 如何使用 flutter 打印 firebase 文档参考屏幕 - How to print firebase document reference to screen using flutter 有没有办法在flutter的android应用程序屏幕中打印调试或控制台消息? - Is there any way to print debug or console message in screen of android app in flutter? flutter 为什么我不能在屏幕上打印设备的位置 - flutter why can't I print device's location on screen Flutter 分享不分享图片 - Flutter share does not share an image 如何将 SQLite 映射数据显示到新的 flutter 屏幕。 我可以将它打印到控制台,但不能在 flutter 屏幕中 - How can I show SQLite mapped data to new flutter screen. I can print it to the console but not in flutter screen
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM