Solved a tricky Flutter rendering issue where ListView items were rebuilding unnecessarily.
The fix was to use const constructors properly:
// Before (causing unnecessary rebuilds)
ListView.builder(
itemBuilder: (context, index) {
return ProductCard(product: products[index]);
},
)
// After (optimized)
ListView.builder(
itemBuilder: (context, index) {
return ProductCard(
key: ValueKey(products[index].id),
product: products[index],
);
},
)
Adding ValueKey prevents Flutter from recreating widgets when the data hasn’t changed. Performance improved by ~40%.