[英]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.