Adding Business Logic to Models

Business logic is simply defined by the functionality in an app and how data can change based on the rules or "logic" of the business.

When we want to associate business logic or core functionality associated with our models, we can define this in the model itself.

This is a common approach for simple to medium sized apps.

We define a static function here allowing us to simulate fetching a list of Location objects from an API. Later, we'll use a real endpoint.

Note: it's important to keep things consistent with single vs double quotes. Use single quotes by default if you can. See this section of the Flutter style guide for examples.

// models/location.dart
// ...
class Location {
// ...
static List<Location> fetchAll() {
return [
Location('Arashiyama Bamboo Grove', 'assets/images/kiyomizu-dera.jpg', [
LocationFact('Summary',
'While we could go on about the ethereal glow and seemingly endless heights of this bamboo grove on the outskirts of Kyoto, the sight\'s pleasures extend beyond the visual realm.'),
LocationFact('How to Get There',
'Kyoto airport, with several terminals, is located 16 kilometres south of the city and is also known as Kyoto. Kyoto can also be reached by transport links from other regional airports.'),
]),
];
}
}

Making Our Code Dynamic

Let's now use our new implementation of Location to fetch the first location we have in our list and inject that data into our screen.

Later, we'll simply load this screen based on the Location the user taps (in some "listing" type screen we have yet to implement).