Closed ccc-dvansevenant closed 2 years ago
Thank you for opening up the issue and bringing it to my attention. I will definitely take a look at it.
I am not able to reproduce the issue. It would be much faster and easier for us to fix the issue if you could provide the following informations.
Sorry for the delay. Thanks for the quick reply. Here's my discovery process. Might be a bit verbose, but I want to cover the bases in case I did something wrong.
Using Android Studio 4.2.2 (was 4.2.1 when this started) Flutter v2.2.3 (was 2.2.2) Mac OS X Catalina
Create new flutter project
Ensure pubspec.yml
has sdk: ">=2.12.0 <3.0.0"
Using image_painter: ^1.0.0-nullsafety.0
Replace main.dart
code with the example code from here: https://pub.dev/packages/image_painter/versions/1.0.0-nullsafety.0/example
Note error regarding missing file assets/sample.jpg
:
E/flutter (16431): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: Unable to load asset: assets/sample.jpg
E/flutter (16431): #0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:224:7)
E/flutter (16431): <asynchronous suspension>
E/flutter (16431): #1 ImagePainterState._resolveAndConvertImage (package:image_painter/src/paint_over_image.dart:211:19)
E/flutter (16431): <asynchronous suspension>
E/flutter (16431):
/assets
, adding a file called sample.jpg
and editing pubspec.yml
and adding:
assets:
(Really, a non-issue, since that part will be replaced by loading an actual image)
Run app on emulator and draw, then save.
Note error message about Future<Uint8List?>
not being a subtype of <FutureOr<Uint8List>
E/flutter ( 4984): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: type 'Future<Uint8List?>' is not a subtype of type 'FutureOr<Uint8List>' in type cast
From the saveImage function:
void saveImage() async {
final image =
await (_imageKey.currentState!.exportImage() as FutureOr<Uint8List>);
final directory = (await getApplicationDocumentsDirectory()).path;
The FutureOr
is not liked.
I removed it in my code.
The line became:
final Uint8List? image = await (_imageKey.currentState!.exportImage());
Which then causes an issue with image
:
final imgFile = File('$fullPath');
imgFile.writeAsBytesSync(image);
ScaffoldMessenger.of(context).showSnackBar(
So, add a null check:
imgFile.writeAsBytesSync(image!);
Running again:
E/flutter ( 6142): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: type 'Future<ByteData?>' is not a subtype of type 'FutureOr<ByteData>' in type cast
E/flutter ( 6142): #0 ImagePainterState.exportImage (package:image_painter/src/paint_over_image.dart:464:5)
E/flutter ( 6142): <asynchronous suspension>
E/flutter ( 6142): #1 _ImagePainterExampleState.saveImage (package:image_painter_null_safety_test/main.dart:57:30)
E/flutter ( 6142): <asynchronous suspension>
E/flutter ( 6142):
Modifying paint_over_image.dart
line 463-464 gets rid of that error:
final byteData = await (\_image.toByteData(format: ui.ImageByteFormat.png)
as FutureOr<ByteData?>);
But introduces an error about buffer
:
/usr/local/Caskroom/flutter/2.0.4/flutter/.pub-cache/hosted/pub.dartlang.org/image_painter-1.0.0-nullsafety.0/lib/src/paint_over_image.dart:465:21: Error: Property 'buffer' cannot be accessed on 'ByteData?' because it is potentially null.
- 'ByteData' is from 'dart:typed_data'.
Try accessing using ?. instead.
return byteData.buffer.asUint8List();
^^^^^^
So, modifying 465, as suggested:
return byteData?.buffer.asUint8List();
Seems to make it all work without complaint. But, again, do not know if it is the right way.
Flutter 2.5.1, dart 2.14.2
@lively-bigyan
I'm working with this library at the moment and can confirm that I have the same issue as @ccc-dvansevenant. Can also confirm that changing the paint_over_image.dart file (line 464/465) from:
final byteData = await (_image.toByteData(format: ui.ImageByteFormat.png)
as FutureOr<ByteData>);
return byteData.buffer.asUint8List();
To:
final byteData = await (_image.toByteData(format: ui.ImageByteFormat.png)
as FutureOr<ByteData?>);
return byteData?.buffer.asUint8List();
Does fix the problem.
Should I create a PR or are you ok fixing this @lively-bigyan ?
@dchrzanowski Thank you for the tests and the solution. I am currently working on some new features of the package so I couldn't push the fixes sooner. I shall fix it today.
On second thought, I just realized that I had it fixed on 0.4.0. Have you guys tried that version? @dchrzanowski @ccc-dvansevenant
On second thought, I just realized that I had it fixed on 0.4.0. Have you guys tried that version? @dchrzanowski @ccc-dvansevenant
I've had to set aside that part of my project for a while, so I haven't updated anything. I'll try to have a look soon. Thanks!
@lively-bigyan I did not realize that 0.4.0 was null safety ready. I'll check it out tomorrow morning so.
On second thought, I just realized that I had it fixed on 0.4.0. Have you guys tried that version? @dchrzanowski @ccc-dvansevenant
0.4.0 works like a charm! Thanks @lively-bigyan!
Not a problem. Glad it helped.
Thanks for this library, and starting to migrate it to null safety.
I am using
image_painter: ^1.0.0-nullsafety.0
.My project is set with:
As I was implementing image_painter in my project, I was having issues with
exportImage
.It kept complaining about ByteData, and then buffer. Eventually I hit upon this combination:
Note the last 2 lines, specifically.
I don't know if it's correct or not, but thought I'd pass it along in case it helps.
I'm still learning, very slowly, so it's certainly possible this could be done better.