Chapter 6: simple question on usage of 'const' on home.dart

Hi, all. Very specific here, but could anyone explain why the ‘const’ is needed for GroceryScreen(), but neither for ExploreScreen() nor for RecipesScreen() on ‘_HomeState extends’ on home.dart?



@helvecio const is actually not necessary, but recommended by flutter. The linter will throw a warning if you don’t add const to GroceryScreen.

The const keyword lets you know that this variable is known at compile time, and never changes. By adding const, it lets flutter know to only rebuild widgets that need updating.

In the case of ExploreScreen() and RecipesScreen(), they both depend on the MockFooderlichService which provides data asynchronously. For example you could modify the content in the JSON, and do a pull to refresh to update the list.

1 Like

hey, @jomoka, many thanks for this, clear and very useful. thanks indeed.

1 Like