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

Time:03-05

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?

CodePudding user response:

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.

  • Related