我有一个表格,在那个表格中我有一个签名板。我使用了签名3.2.0包。该程序包包含一个toImage()方法。我想将该图像存储在Firebase存储中。当我尝试下面的代码时。
fileImage = _controller.toImage() as File;
final FirebaseStorage storage = FirebaseStorage.instance;
final String picture = "${DateTime.now().millisecondsSinceEpoch.toString()}.jpg";
StorageUploadTask task = storage.ref().child(picture).putFile(fileImage);
task.onComplete.then((snapshot) async{
loadData.setSignatureURL = await snapshot.ref.getDownloadURL();
});
loadData.storeDetails();
我得到了一个错误类型'图像'不是强制类型转换中'文件'类型的子类型。 如何将签名存储为图像/
答案 0 :(得分:1)
无法立即进行铸造,这就是为什么会出现此错误。
Image
类提供了一种toByteData
方法,该方法使您可以将原始图像数据作为ByteData
对象来检索。然后,您可以将其转换为Uint8List
。然后,该列表可以使用putData
方法代替putFile
直接用于Firebase存储。
var image = await _controller.toImage();
ByteData data = await image.toByteData();
Uint8List listData = data.buffer.asUint8List();
final FirebaseStorage storage = FirebaseStorage.instance;
final String picture = "${DateTime.now().millisecondsSinceEpoch.toString()}.jpg";
StorageUploadTask task = storage.ref().child(picture).putData(listData);
...
如果您需要将此图像编码为特定类型。您可以使用以下编码为JPG的版本。它使用image
程序包,该程序包需要作为依赖项添加
import 'package:image/image.dart' as encoder;//This import needs to be added in the file this is being done
var image = await _controller.toImage();
//Store image dimensions for later
int height = image.height;
int width = image.width;
ByteData data = await image.toByteData();
Uint8List listData = data.buffer.asUint8List();
encoder.Image toEncodeImage = encoder.Image.fromBytes(width, height, listData);
encoder.JpegEncoder jpgEncoder = encoder.JpegEncoder();
List<int> encodedImage = jpgEncoder.encodeImage(toEncodeImage);
final FirebaseStorage storage = FirebaseStorage.instance;
final String picture = "${DateTime.now().millisecondsSinceEpoch.toString()}.jpg";
StorageUploadTask task = storage.ref().child(picture).putData(Uint8List.fromList(encodedImage));
...