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

Mapping

In the first example, we used the map function. map expects you to take the data element as the input of your function and then transform it into something else. It is very common to map some JSON data that your app received from an API to a strongly typed Dart object:

// Without the map function, we would usually write
// code like this
final
names = <Name>[];
for (Map rawName in data) {
final first = rawName['first'];
final last = rawName['last'];
final name = Name(first, last);
names.add(name);
}

// But instead it can be simplified and it can
// actually be more performant on more complex data
final names = data.map<Name>((Map rawName) {
final first = rawName['first'];
final last = rawName['last'];
return Name(first, last);
}).toList();

Both samples achieve the same result. In the first option, you create a list that will hold the names. Then, you iterate through each entry in the data list, extract the elements from Map, initialize a named object, and then add it to the list.

The second option is certainly easier for the developer. Iterating and adding are delegated to the map function. All you need to do is tell the map function how you want to transform the element. In this case, the transformation was extracting the values and returning a Name object. map is also a generic function. Consequently, you can add some typing information  in this case, <Name> – to tell Dart that you want to save a list of names, not a list of dynamics.

This example is also purposefully verbose, although you could simplify it even more:

final names = data.map<Name>(
(raw) => Name(raw['first'], raw['last']),
).toList();

This may not seem like a big deal for this simple example, but when you need to parse complex graphs of data, these techniques can save you a lot of work and time.

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 ₹800/month. Cancel anytime