You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Lets say i have a page with a list of widgets in a Column and some of them change when the state of the app changes.
Widgetbuild(BuildContext context) {
var value =Provider.of<Data>(context);
returnColumn(
children: [
// Data that changes and needs to be updatedText("data that changes $value"), // widgetAText("More data that changes $value"), //widgetB// Data that doesnt change but is expensive to buildText("data that doesnt change"), // widgetCText("More data that doesnt change"), //widgetD
],
);
}
to improve performance i could wrap WidgetA and WidgetB each in a new Consumer
Widgetbuild(BuildContext context) {
returnColumn(
children: [
// Data that changes and needs to be updatedConsumer<Data>(builder: (context, value, child) =>Text("data that changes $value")), // widgetAConsumer<Data>(builder: (context, value, child) =>Text("More data that changes $value")), //widgetB// Data that doesnt change but is expensive to buildText("data that doesnt change"), // widgetCText("More data that doesnt change"), //widgetD
],
);
}
but now we have a bit code duplication
Proposal
to minimize code duplication i would suggest a pattern like
Widgetbuild(BuildContext context) {
returnConsumer<Data>(
builder: (context, value, child, children) =>Column(
children: [
Text("data that changes $value"),
Text("More data that changes $value"),
...children,
child,
],
children: [
Text("data that doesnt change"), // widgetCText("More data that doesnt change"), //widgetD
],
),
);
}
or with a new class ConsumerList which is basicly a List of Widgets. (Could be also a function)
Widgetbuild(BuildContext context) {
returnColumn(
children: [
// Data that changes and needs to be updated
...ConsumerList<Data>(builder:(context, value) => [
Text("data that changes $value"),
Text("More data that changes $value"),
]),
// Data that doesnt change but is expensive to buildText("data that doesnt change"), // widgetCText("More data that doesnt change"), //widgetD
],
);
}
or
Widgetbuild(BuildContext context) {
returnColumn(
children:ConsumerList<Data>(
builder:(context, value, child, children) => [
Text("data that changes $value"),
Text("More data that changes $value"),
...children,
child,
],
children: [
Text("data that doesnt change"), // widgetCText("More data that doesnt change"), //widgetD
],
),
);
}
The text was updated successfully, but these errors were encountered:
To begin with, it's very rare that both of your Text will want to listen to the exact same thing and that they are this small.
Usually you'll either want to use select or make a separate widget anyway
I agree with you, that this is probably rarely used since the problem is the result of how i designed my code. It might be better to change the design instead of making the frame work fit my design.
Use case
Lets say i have a page with a list of widgets in a Column and some of them change when the state of the app changes.
to improve performance i could wrap WidgetA and WidgetB each in a new Consumer
but now we have a bit code duplication
Proposal
to minimize code duplication i would suggest a pattern like
or with a new class ConsumerList which is basicly a List of Widgets. (Could be also a function)
or
The text was updated successfully, but these errors were encountered: