Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying to solve code duplication when working with lists of Widgets in Consumer #850

Closed
Raphael2b3 opened this issue Dec 13, 2023 · 4 comments
Assignees
Labels
enhancement New feature or request

Comments

@Raphael2b3
Copy link

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.

  Widget build(BuildContext context) {
    var value = Provider.of<Data>(context);
    return Column(
      children: [
        // Data that changes and needs to be updated
        Text("data that changes $value"),  // widgetA
        Text("More data that changes $value"), //widgetB
        
        // Data that doesnt change but is expensive to build
        Text("data that doesnt change"), // widgetC
        Text("More data that doesnt change"), //widgetD
        
       ],
    );
  }

to improve performance i could wrap WidgetA and WidgetB each in a new Consumer

  Widget build(BuildContext context) {
   
    return Column(
      children: [
        // Data that changes and needs to be updated
        Consumer<Data>(builder: (context, value, child) => Text("data that changes $value")),  // widgetA
        Consumer<Data>(builder: (context, value, child) => Text("More data that changes $value")), //widgetB
        
        // Data that doesnt change but is expensive to build
        Text("data that doesnt change"), // widgetC
        Text("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

  Widget build(BuildContext context) {
    return Consumer<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"), // widgetC
            Text("More data that doesnt change"), //widgetD
         ],
       ),
    );
  }

or with a new class ConsumerList which is basicly a List of Widgets. (Could be also a function)

  Widget build(BuildContext context) {
   
    return Column(
      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 build
        Text("data that doesnt change"), // widgetC
        Text("More data that doesnt change"), //widgetD
        
       ],
    );
  }

or

Widget build(BuildContext context) {
    return Column(
      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"), // widgetC
            Text("More data that doesnt change"), //widgetD
         ],
       ),
    );
  }
@rrousselGit
Copy link
Owner

rrousselGit commented Dec 13, 2023

I'm not convinced this problem is worth solving.

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

@Raphael2b3
Copy link
Author

Do you have a reference for select. I am not sure what you mean with it.

@rrousselGit
Copy link
Owner

context.select

@Raphael2b3
Copy link
Author

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.

@Raphael2b3 Raphael2b3 closed this as not planned Won't fix, can't repro, duplicate, stale Dec 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants