In Consumer builder, is there any different between using child and const?

Issue

I known that child argument is use to tell builder to not re-build that widget every time data change.

But isn’t that the defenition of const? why not use const instead.

Consumer<String>(
  builder: (context, value, child) => Badge(
    child: const IconButton(
      icon: Icon(Icons.shopping_bag),
      onPressed: () {},
    ),
    badgeContent: Text(value),
  ),
)

vs

Consumer<String>(
  builder: (context, value, child) => Badge(
    child: child,
    badgeContent: Text(value),
  ),
  child: IconButton(
    icon: Icon(Icons.shopping_bag),
    onPressed: () {},
  )
)

Is there any different?

Solution

Whatever widget you pass as a child will not rebuild when data is changed.

You can avoid unnecessary rebuilding of widgets using child.

Whether it is a const Widget or a normal Widget when you keep it in the child then the widget won’t rebuild when changes occur.

Answered By – nagendra nag

Answer Checked By – Gilberto Lyons (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *