Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Flutter Cookbook

You're reading from   Flutter Cookbook Over 100 proven techniques and solutions for app development with Flutter 2.2 and Dart

Arrow left icon
Product type Paperback
Published in Jun 2021
Publisher Packt
ISBN-13 9781838823382
Length 646 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Simone Alessandria Simone Alessandria
Author Profile Icon Simone Alessandria
Simone Alessandria
Brian Kayfitz Brian Kayfitz
Author Profile Icon Brian Kayfitz
Brian Kayfitz
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface 1. Getting Started with Flutter 2. Dart: A Language You Already Know FREE CHAPTER 3. Introduction to Widgets 4. Mastering Layout and Taming the Widget Tree 5. Adding Interactivity and Navigation to Your App 6. Basic State Management 7. The Future is Now: Introduction to Asynchronous Programming 8. Data Persistence and Communicating with the Internet 9. Advanced State Management with Streams 10. Using Flutter Packages 11. Adding Animations to Your App 12. Using Firebase 13. Machine Learning with Firebase ML Kit 14. Distributing Your Mobile App 15. Flutter Web and Desktop 16. About Packt

Iterables and chaining higher-order functions

If you inspected the source code of the map and where functions, you probably noticed that the return type of these functions is not a List, but another type called Iterable. This abstract class represents an intermediary state before you decide what concrete data type you want to store. It doesn't necessarily have to be a List. You can also convert your iterable into a Set if you want.

The advantage of using iterables is that they are lazy. Programming is one of the only professions where laziness is a virtue. In this context, laziness means that the function will only be executed when it's needed, not earlier. This means that we can take multiple higher-order functions and chain them together, without stressing the processor with unnecessary cycles.

We could reduce the sample code even further and add more functions for good measure:

final names = data
.map<Name>((raw) => Name(raw['first'], raw['last']))
.where((name) => name.last.startsWith('M'))
.where((name) => name.first.length > 5)
.toList(growable: false);

Each of these functions is cached in our Iterable and only runs when you make the call to toList(). Here, you are serializing the data in a model, checking whether the last name starts with M, and then checking whether the first name is longer than five letters. This is executed in a single iteration through the list!

You have been reading a chapter from
Flutter Cookbook
Published in: Jun 2021
Publisher: Packt
ISBN-13: 9781838823382
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime