共享和打印屏幕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';

导入。。。

screenshot: ^0.2.0
esys_flutter_share: ^1.0.2

我只需要拍摄一张照片,然后分享它。我正在使用以下代码,但我收到了错误:

_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);
});
}

我的错误是:

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

我在截屏包中遇到了同样的问题,所以我的解决方法是在出现错误时再次调用相同的函数。

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);
});

最新更新