In this lesson we'll cover:
map()
, Anonymous Functions and Cascades in DartCheck out the tourismandco repo's step/step05
branch for the code we'll cover in this lesson.
Location
and a list of TextSection
widgets for each "Location Facts" that it relates to.location.dart
// models/location.dart
import './location_fact.dart';
/// Represents a tourism location a user can visit.
class Location {
final String name;
final String imagePath;
final List<LocationFact> facts;
Location(this.name, this.imagePath, this.facts);
// NOTE: implementing functionality here in the next step!
}
List<LocationFact>
type.LocationFact
, for a given higher level data type, in this case, List
.LocationFact
in our list, a compiler error will be thrown.location_fact.dart
// models/location_fact.dart
/// Represents some descriptive information about a [Location].
class LocationFact {
final String title;
final String text;
LocationFact(this.title, this.text);
}
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.'),
]),
];
}
}
Location
to fetch the first location we have in our list and inject that data into our screen.location_detail.dart
// screens/location_detail/location_detail.dart
import 'package:flutter/material.dart';
import '../../models/location.dart';
import 'image_banner.dart';
import 'text_section.dart';
class LocationDetail extends StatelessWidget {
@override
Widget build(BuildContext context) {
// simply fetch the first location we have
// NOTE: we'll be moving this to a scoped_model later
final locations = Location.fetchAll();
final location = locations.first;
return Scaffold(
appBar: AppBar(
title: Text(location.name),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
ImageBanner(location.imagePath),
]..addAll(textSections(location))),
);
}
List<Widget> textSections(Location location) {
return location.facts
.map((fact) => TextSection(fact.title, fact.text))
.toList();
}
}
[]
list, before it's assigned to the children
parameter, has addAll()
called on it.addAll()
method simply appends a list onto an existing list....
) will be available, allowiing us to forego the addAll()
method.map()
and Anonymous Functionsmap()
(read up on it here) allows us to take an existing list, iterate over that list and change every item using a function.(fact)
here is a parameter, =>
denotes the one-liner functionality we can use.main.dart
as void main() => runApp(App());
map()
doesn't return a List
, we need to called toList()
.NOTE: you don't need to master these features of Dart early on. They're used quite often enough so you should naturally get used to them as you write more Dart code.
In this lesson: