I was trying to use the palette generator plugin by flutter team. However, when I run the function, the UI freezes for two seconds or three. Here is how im trying to use it:
void _generateColorPalette(BuildContext context) async {
try {
if (this.imageData == null) {
return;
}
setState(() {
isLoading = true;
});
final PaletteGenerator _temp = await PaletteGenerator.fromImageProvider(
MemoryImage(imageData as Uint8List));
setState(() {
isLoading = false;
pallete = _temp.colors.toList();
});
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Pallete Generated!'),
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(50.0),
),
),
);
} catch (e) {
setState(() {
isLoading = false;
});
print('Error occured while generating the palette: $e');
}
}
I also tried using it in a compute function but to no avail cause i get this error:
Restarted application in 2,281ms.
I/Timeline( 4948): Timeline: Activity_launch_request time:96984061
W/Activity( 4948): Slow Operation: Activity com.example.glitter/.MainActivity onActivityResult took 187ms
I/flutter ( 4948): ══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE ╞════════════════════════════════════════════════════
I/flutter ( 4948): The following _CastError was thrown while resolving an image:
I/flutter ( 4948): Null check operator used on a null value
I/flutter ( 4948):
I/flutter ( 4948): When the exception was thrown, this was the stack:
I/flutter ( 4948): #0 ImageProvider.resolveStreamForKey
I/flutter ( 4948): #1 ImageProvider.resolve.<anonymous closure>
I/flutter ( 4948): #2 ImageProvider._createErrorHandlerAndKey.<anonymous closure>.<anonymous closure>
I/flutter ( 4948): #3 SynchronousFuture.then
I/flutter ( 4948): #4 ImageProvider._createErrorHandlerAndKey.<anonymous closure>
I/flutter ( 4948): #8 ImageProvider._createErrorHandlerAndKey
I/flutter ( 4948): #9 ImageProvider.resolve
I/flutter ( 4948): #10 PaletteGenerator.fromImageProvider
I/flutter ( 4948): #11 _ImageScreenState.generatePallete
I/flutter ( 4948): #12 _IsolateConfiguration.apply
I/flutter ( 4948): #13 _spawn.<anonymous closure>
I/flutter ( 4948): #14 _spawn.<anonymous closure>
I/flutter ( 4948): #15 Timeline.timeSync (dart:developer/timeline.dart:163:22)
I/flutter ( 4948): #16 _spawn
I/flutter ( 4948): #17 _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:286:17)
I/flutter ( 4948): (elided 4 frames from class _RawReceivePortImpl and dart:async)
I/flutter ( 4948):
I/flutter ( 4948): Image provider: MemoryImage(Uint8List#e9679, scale: 1.0)
I/flutter ( 4948): Image configuration: ImageConfiguration(devicePixelRatio: 1.0)
I/flutter ( 4948): Image key: MemoryImage(Uint8List#e9679, scale: 1.0)
I/flutter ( 4948): ════════════════════════════════════════════════════════════════════════════════════════════════════
I/flutter ( 4948): Error occured: TimeoutException: Timeout occurred trying to load from MemoryImage(Uint8List#e9679, scale: 1.0)
Reloaded 1 of 949 libraries in 1,408ms.
I/flutter ( 4948): ══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE ╞════════════════════════════════════════════════════
I/flutter ( 4948): The following _CastError was thrown while resolving an image:
I/flutter ( 4948): Null check operator used on a null value
I/flutter ( 4948):
I/flutter ( 4948): When the exception was thrown, this was the stack:
I/flutter ( 4948): #0 ImageProvider.resolveStreamForKey
I/flutter ( 4948): #1 ImageProvider.resolve.<anonymous closure>
I/flutter ( 4948): #2 ImageProvider._createErrorHandlerAndKey.<anonymous closure>.<anonymous closure>
I/flutter ( 4948): #3 SynchronousFuture.then
I/flutter ( 4948): #4 ImageProvider._createErrorHandlerAndKey.<anonymous closure>
I/flutter ( 4948): #8 ImageProvider._createErrorHandlerAndKey
I/flutter ( 4948): #9 ImageProvider.resolve
I/flutter ( 4948): #10 PaletteGenerator.fromImageProvider
I/flutter ( 4948): #11 _ImageScreenState.generatePallete
I/flutter ( 4948): #12 _IsolateConfiguration.apply
I/flutter ( 4948): #13 _spawn.<anonymous closure>
I/flutter ( 4948): #14 _spawn.<anonymous closure>
I/flutter ( 4948): #15 Timeline.timeSync (dart:developer/timeline.dart:163:22)
I/flutter ( 4948): #16 _spawn
I/flutter ( 4948): #17 _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:286:17)
I/flutter ( 4948): (elided 4 frames from class _RawReceivePortImpl and dart:async)
I/flutter ( 4948):
I/flutter ( 4948): Image provider: MemoryImage(Uint8List#53605, scale: 1.0)
I/flutter ( 4948): Image configuration: ImageConfiguration(devicePixelRatio: 1.0)
I/flutter ( 4948): Image key: MemoryImage(Uint8List#53605, scale: 1.0)
I/flutter ( 4948): ════════════════════════════════════════════════════════════════════════════════════════════════════
I/flutter ( 4948): Error occured: TimeoutException: Timeout occurred trying to load from MemoryImage(Uint8List#53605, scale: 1.0)
Reloaded 2 of 949 libraries in 1,273ms.
Heres how I was trying to use it in isolate:
void _generateColorPalette(BuildContext context) async {
try {
if (this.imageData == null) {
return;
}
setState(() {
isLoading = true;
});
// final PaletteGenerator _temp = await PaletteGenerator.fromImageProvider(
// MemoryImage(imageData as Uint8List));
final List<Color>? _temp =
await compute(generatePalette, imageData as Uint8List);
if (_temp == null) {
return;
}
setState(() {
isLoading = false;
// pallete = _temp.colors.toList();
pallete = _temp;
});
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Pallete Generated!'),
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(50.0),
),
),
);
} catch (e) {
setState(() {
isLoading = false;
});
print('Error occured while generating the palette: $e');
}
}
and the isolated function:
Future<List<Color>?> generatePalette(Uint8List _image) async {
try {
final PaletteGenerator _generator =
await PaletteGenerator.fromImageProvider(MemoryImage(_image));
return _generator.colors.toList();
} catch (e) {
print('Error occured while generating palette in the isolate: $e');
}
}
Any ideas on what might be missing?