Chapter 6.4 Best Practice for use of inline constants vs file containing constants?

In Chapter 6.4, where we say

// 1
Flexible(
  // 2
  child: AspectRatio(
    aspectRatio: 1 / 1,
    child: Image.asset('assets/fooderlich_assets/empty_list.png'),
  ),
),

would best practice be to have a separate constants.dart file that has an entry something like

const String empty_list_graphic = 'assets/fooderlich_assets/empty_list.png'

and then replace the child with

child: Image.asset(empty_list_graphic),

or something like the above? That way, we could move either the file or directory or rename it or change it from a png to jpg or find where it’s used?

@jefff9511 yes that’s a good point.

If you are thinking about a bigger project, you are right. It may be a good idea to create some shared manager which holds your constants so you can access it wherever you need.

Advantages of this approach is:

  • Less prone for error, like typos
  • Constants all in one place easy to manage and add to
  • Also unit testable, if you want to test the path and file name is correct