Catching common JSON errors
As you saw in the previous recipe, you need to deal with JSON data that may be incompatible with your own data. But there is another source of errors that comes from within your code.
- When dealing with JSON, it often happens that the key names are repeated several times in your code. Since we are not perfect, we might create an error by typing in something that's incorrect. It's rather difficult to handle these kinds of errors as they only occur at runtime.
- A common way to prevent these kinds of typos is to use constants instead of typing the key names each time you need to reference them.
Getting ready
To follow along with this recipe, you will need to have completed the previous recipe.
How to do it...
In this recipe, we will use constants instead of typing in the keys of each field of the JSON data. The starting code is at the end of the previous recipe:
- At the top of the
pizza.dart
file (before the Pizza class), add the constants...