SachinGanesh / screenshot

Flutter Screenshot Library
https://pub.dev/packages/screenshot
MIT License
327 stars 137 forks source link

captureFromWidget with Provider: ProviderNotFoundException #144

Closed xErik closed 1 year ago

xErik commented 1 year ago

Capturing a widget using a Provider crashes when using captureFromWidget.

The workaround would be to set parameters in the Widget constructor instead of using a Provider internally.

However, I wonder if there is a way to make it work without refactoring the Provider out off the widget.

final pngBytes =
  await ScreenshotController().captureFromWidget(const ImageText());
@override
Widget build(BuildContext context) {
  return Consumer<ModelImage>(builder: (context, model, child) {
    // ...
  });
}
The following ProviderNotFoundException was thrown building Consumer<ModelImage>(dirty):
Error: Could not find the correct Provider<ModelImage> above this Consumer<ModelImage> Widget
This happens because you used a `BuildContext` that does not include the provider of your choice. 
rmahmadkhan commented 10 months ago

@xErik you opened this issue and closed on the same day. Did you find a better solution or it worked for you?

xErik commented 10 months ago

@rmahmadkhan You need to avoid wrapping the Consumer with the Screenshot. Place it above Screenshot in the Widget tree.