Closed afl-dev closed 2 years ago
Hey, do you get some error there?
Cause it's working on the demo app here: https://ookami-kb.github.io/storybook_flutter/#/
Storybook(
plugins: initializePlugins(
contentsSidePanel: true,
knobsSidePanel: true,
),
initialStory: 'Simple/ActionButton',
stories: [
_parents(context, _iconButton(context)),
],
);
}
Widget _iconButton(BuildContext context) {
return simple.IconButton(
iconSize: context.knobs.slider(label: 'iconSize', max: 256, initial: 24),
splashRadius:
context.knobs.slider(label: 'splashRadius', max: 256, initial: 24),
icon: context.knobs.options(
label: 'Icons',
initial: Icons.arrow_back,
options: const [
Option(
label: 'Icons.arrow_back',
value: Icons.arrow_back,
description: 'icon',
),
Option(
label: 'Icons.portrait',
value: Icons.portrait,
description: 'icon',
),
],
),
iconButtonStyle: simple.IconButtonStyle(
color: context.knobs.options(
label: 'IconButton color',
initial: Colors.blue,
options: const [
Option(
label: 'Colors.blue',
value: Colors.blue,
description: 'Blue color',
),
Option(
label: 'Colors.green',
value: Colors.green,
description: 'Green color',
),
],
),
disabledColor: context.knobs.options(
label: 'disabledColor',
initial: Colors.black12,
options: const [
Option(
label: 'Colors.black12',
value: Colors.black12,
),
Option(
label: 'Colors.grey',
value: Colors.grey,
),
],
),
),
onPressed:
context.knobs.boolean(label: 'enable', initial: true) ? () {} : null,
actionInProgress: context.knobs.boolean(label: 'actionInProgress'),
);
}
======== Exception caught by widgets library =======================================================
The following ProviderNotFoundException was thrown building MyApp(dirty):
Error: Could not find the correct Provider
This happens because you used a BuildContext
that does not include the provider
of your choice. There are a few common scenarios:
You added a new provider in your main.dart
and performed a hot-reload.
To fix, perform a hot-restart.
The provider you are trying to read is in a different route.
Providers are "scoped". So if you insert of provider inside a route, then other routes will not be able to access that provider.
You used a BuildContext
that is an ancestor of the provider you are trying to read.
Make sure that MyApp is under your MultiProvider/Provider
For example, instead of:
Widget build(BuildContext context) {
return Provider<Example>(
create: (_) => Example(),
// Will throw a ProviderNotFoundError, because `context` is associated
// to the widget that is the parent of `Provider<Example>`
child: Text(context.watch<Example>()),
),
}
consider using builder
like so:
Widget build(BuildContext context) {
return Provider<Example>(
create: (_) => Example(),
// we use `builder` to obtain a new `BuildContext` that has access to the provider
builder: (context) {
// No longer throws
return Text(context.watch<Example>()),
}
),
}
If none of these solutions work, consider asking for help on StackOverflow: https://stackoverflow.com/questions/tagged/flutter
The relevant error-causing widget was:
When the exception was thrown, this was the stack:
======== Exception caught by widgets library =======================================================
The following ProviderNotFoundException was thrown building MyApp(dirty):
Error: Could not find the correct Provider
This happens because you used a BuildContext
that does not include the provider
of your choice. There are a few common scenarios:
You added a new provider in your main.dart
and performed a hot-reload.
To fix, perform a hot-restart.
The provider you are trying to read is in a different route.
Providers are "scoped". So if you insert of provider inside a route, then other routes will not be able to access that provider.
You used a BuildContext
that is an ancestor of the provider you are trying to read.
Make sure that MyApp is under your MultiProvider/Provider
For example, instead of:
Widget build(BuildContext context) {
return Provider<Example>(
create: (_) => Example(),
// Will throw a ProviderNotFoundError, because `context` is associated
// to the widget that is the parent of `Provider<Example>`
child: Text(context.watch<Example>()),
),
}
consider using builder
like so:
Widget build(BuildContext context) {
return Provider<Example>(
create: (_) => Example(),
// we use `builder` to obtain a new `BuildContext` that has access to the provider
builder: (context) {
// No longer throws
return Text(context.watch<Example>()),
}
),
}
If none of these solutions work, consider asking for help on StackOverflow: https://stackoverflow.com/questions/tagged/flutter
The relevant error-causing widget was:
When the exception was thrown, this was the stack:
Can you please share how you create the
Storybook
widget?
please see above
Yeah, I see it now.
_parents(context, _iconButton(context)),
It won't work like this. You shouldn't pass the context to the function creating the Widget. When you're using Story()
constructor, you've got a builder
parameter that passes the right context – you should use it to get access to knobs.
Closing the issue, working as expected.
storybook_flutter: ^0.10.0+1 sample