[{"data":1,"prerenderedAt":4454},["ShallowReactive",2],{"navigation_docs_en":3,"-en-flutter-for-jobseekers-working-with-apis-and-async":95,"-en-flutter-for-jobseekers-working-with-apis-and-async-surround":4449},[4,16],{"title":5,"icon":6,"path":7,"stem":8,"children":9,"page":15},"Getting Started","i-lucide-rocket","\u002Fen\u002Fgetting-started","en\u002F1.getting-started",[10],{"title":11,"path":12,"stem":13,"icon":14},"Introduction","\u002Fen\u002Fgetting-started\u002Fintroduction","en\u002F1.getting-started\u002F1.introduction","i-lucide-house",false,{"title":17,"icon":18,"path":19,"stem":20,"children":21,"page":15},"Flutter for Jobseekers","i-lucide-briefcase","\u002Fen\u002Fflutter-for-jobseekers","en\u002F2.flutter-for-jobseekers",[22,27,32,37,42,47,52,57,62,67,72,77,82,86,91],{"title":23,"path":24,"stem":25,"icon":26},"Introduction to Flutter","\u002Fen\u002Fflutter-for-jobseekers\u002Fintroduction-to-flutter","en\u002F2.flutter-for-jobseekers\u002F1.introduction-to-flutter","i-lucide-history",{"title":28,"path":29,"stem":30,"icon":31},"Market Opportunities for Flutter Developers","\u002Fen\u002Fflutter-for-jobseekers\u002Fmarket-opportunities","en\u002F2.flutter-for-jobseekers\u002F2.market-opportunities","i-lucide-trending-up",{"title":33,"path":34,"stem":35,"icon":36},"Installing Flutter and Configuring Your IDE","\u002Fen\u002Fflutter-for-jobseekers\u002Finstalling-flutter","en\u002F2.flutter-for-jobseekers\u002F3.installing-flutter","i-lucide-wrench",{"title":38,"path":39,"stem":40,"icon":41},"Introduction to Widgets","\u002Fen\u002Fflutter-for-jobseekers\u002Fintroduction-to-widgets","en\u002F2.flutter-for-jobseekers\u002F4.introduction-to-widgets","i-lucide-blocks",{"title":43,"path":44,"stem":45,"icon":46},"Handling User Input","\u002Fen\u002Fflutter-for-jobseekers\u002Fhandling-user-input","en\u002F2.flutter-for-jobseekers\u002F5.handling-user-input","i-lucide-text-cursor-input",{"title":48,"path":49,"stem":50,"icon":51},"Using 3rd Party Libraries and External Assets","\u002Fen\u002Fflutter-for-jobseekers\u002Fusing-packages-and-assets","en\u002F2.flutter-for-jobseekers\u002F6.using-packages-and-assets","i-lucide-package",{"title":53,"path":54,"stem":55,"icon":56},"Working with APIs and Asynchronous Operations","\u002Fen\u002Fflutter-for-jobseekers\u002Fworking-with-apis-and-async","en\u002F2.flutter-for-jobseekers\u002F7.working-with-apis-and-async","i-lucide-cloud-download",{"title":58,"path":59,"stem":60,"icon":61},"Navigation and Routing","\u002Fen\u002Fflutter-for-jobseekers\u002Fnavigation-and-routing","en\u002F2.flutter-for-jobseekers\u002F8.navigation-and-routing","i-lucide-route",{"title":63,"path":64,"stem":65,"icon":66},"State Management and the BLoC","\u002Fen\u002Fflutter-for-jobseekers\u002Fstate-management-and-bloc","en\u002F2.flutter-for-jobseekers\u002F9.state-management-and-bloc","i-lucide-boxes",{"title":68,"path":69,"stem":70,"icon":71},"Reactivity and Platform-Specific Considerations","\u002Fen\u002Fflutter-for-jobseekers\u002Freactivity-and-platforms","en\u002F2.flutter-for-jobseekers\u002F910.reactivity-and-platforms","i-lucide-monitor-smartphone",{"title":73,"path":74,"stem":75,"icon":76},"Debugging, Troubleshooting, and Performance Considerations","\u002Fen\u002Fflutter-for-jobseekers\u002Fdebugging-troubleshooting-performance","en\u002F2.flutter-for-jobseekers\u002F911.debugging-troubleshooting-performance","i-lucide-bug",{"title":78,"path":79,"stem":80,"icon":81},"Creating Your First Application","\u002Fen\u002Fflutter-for-jobseekers\u002Fcreating-your-first-application","en\u002F2.flutter-for-jobseekers\u002F912.creating-your-first-application","i-lucide-cloud-sun",{"title":83,"path":84,"stem":85,"icon":18},"Finding Flutter Jobs","\u002Fen\u002Fflutter-for-jobseekers\u002Ffinding-flutter-jobs","en\u002F2.flutter-for-jobseekers\u002F913.finding-flutter-jobs",{"title":87,"path":88,"stem":89,"icon":90},"Preparing for and Succeeding in the Job Interview","\u002Fen\u002Fflutter-for-jobseekers\u002Fpreparing-for-the-job-interview","en\u002F2.flutter-for-jobseekers\u002F914.preparing-for-the-job-interview","i-lucide-messages-square",{"title":92,"path":93,"stem":94,"icon":61},"Your Road Ahead","\u002Fen\u002Fflutter-for-jobseekers\u002Fyour-road-ahead","en\u002F2.flutter-for-jobseekers\u002F915.your-road-ahead",{"id":96,"title":53,"body":97,"description":4442,"extension":4443,"links":4444,"meta":4445,"navigation":4446,"path":54,"seo":4447,"stem":55,"__hash__":4448},"docs_en\u002Fen\u002F2.flutter-for-jobseekers\u002F7.working-with-apis-and-async.md",{"type":98,"value":99,"toc":4395},"minimark",[100,115,123,149,154,206,209,224,229,232,331,345,348,379,390,394,397,467,474,477,504,541,548,551,561,607,635,657,668,676,679,686,709,717,721,724,734,752,777,782,789,799,808,812,822,825,905,912,916,919,995,1008,1019,1022,1029,1033,1040,1048,1058,1120,1135,1150,1157,1160,1228,1235,1262,1268,1285,1349,1362,1386,1399,1419,1423,1436,1519,1530,1533,1571,1578,1581,1588,1674,1682,1700,1752,1776,1779,1803,1813,1817,1841,1848,1891,1898,1901,1904,1915,2056,2083,2088,2097,2148,2166,2176,2185,2212,2215,2314,2320,2353,2366,2393,2398,2404,2453,2467,2492,2506,2564,2578,2585,2608,2775,2779,2806,2812,2920,2940,2956,2964,3085,3112,3119,3122,3137,3161,3179,3189,3194,3206,3253,3266,3277,3315,3355,3361,3383,3423,3435,3442,3464,3476,3481,3506,3521,3533,3567,3598,3793,3810,3816,3820,3831,3946,3956,3963,3967,3983,3990,4004,4008,4141,4145,4213,4228,4232,4247,4261,4265,4391],[101,102,103,107],"u-page-hero",{},[104,105,53],"template",{"v-slot:title":106},"",[104,108,109,110,114],{"v-slot:description":106},"These days, most applications communicate with services on the internet in some way or another. This generally happens by communicating with the service's ",[111,112,113],"strong",{},"API",", or Application Programming Interface, via HTTP(s) communications.",[116,117,118,119,122],"p",{},"Since we have no control over how long that communication may take, we need to build our applications in such a way that the application is not blocked from further interaction and processing while we wait for a response. We do this by a method called ",[111,120,121],{},"asynchronous programming",". In this chapter, we will delve into asynchronous programming and learn how to communicate with APIs.",[124,125,126,129,130,134,135,138,139,148],"note",{},[111,127,128],{},"Companion code:"," the publisher's repository holds this chapter's code as ",[131,132,133],"code",{},"Example 7.1","–",[131,136,137],{},"Example 7.28"," in the ",[140,141,145],"a",{"href":142,"rel":143},"https:\u002F\u002Fgithub.com\u002Fbpbpublications\u002FFlutter-for-Jobseekers\u002Ftree\u002Fmain\u002FChapter%2007",[144],"nofollow",[131,146,147],{},"Chapter 07"," folder; from Example 7.15 on, each example is the whole app up to that point, so each code block below is labelled with the first example that contains it. The repository has conversion glitches of its own, so the code on this page follows the book (BPB Publications, n.d.).",[150,151,153],"h2",{"id":152},"chapter-overview","Chapter overview",[155,156,157,184,189],"card-group",{},[158,159,162],"card",{"icon":160,"title":161},"i-lucide-list","Structure",[163,164,165,169,172,175,178,181],"ul",{},[166,167,168],"li",{},"Welcome to the Future",[166,170,171],{},"Requesting data from an API",[166,173,174],{},"Classifying API responses",[166,176,177],{},"Sending data to an API",[166,179,180],{},"Flutter's asynchronous widgets",[166,182,183],{},"Sinks and streams",[158,185,188],{"icon":186,"title":187},"i-lucide-target","Objectives","After completing this chapter, you will have a fundamental understanding of concurrency in programming. You will know the difference between synchronous and asynchronous operations and how each style of code can impact the performance of your application. Furthermore, you will learn what APIs are and how to interact with them. Additionally, you will learn about data streams and how to use them in your code. Finally, you will use this knowledge to build a simple application that streams data from an API to build a list in Flutter, and then you will learn how to create new items on that list by sending data to the API and listening for a response.",[158,190,193,194,197,198,201,202,205],{"icon":191,"title":192},"i-lucide-folder-code","Runnable example","The chapter's album app (both the ",[131,195,196],{},"FutureBuilder"," and the ",[131,199,200],{},"StreamController"," version) and its pure-Dart examples are also available as a runnable project with tests in ",[131,203,204],{},"examples\u002Fflutter_for_jobseekers_ch07\u002F",". The code on this page is shown as the book prints it, with conversion glitches fixed.",[150,207,168],{"id":208},"welcome-to-the-future",[116,210,211,212,215,216,218,219,223],{},"To understand asynchronous operations, the first concept we need to learn about is ",[131,213,214],{},"Future","s. As the name suggests, a ",[131,217,214],{}," is something that occurs in the future. It is a ",[220,221,222],"em",{},"promise"," that something will eventually happen.",[225,226,228],"h3",{"id":227},"synchronous-code","Synchronous code",[116,230,231],{},"Let us start by writing some synchronous code to see what that looks like and how it functions:",[233,234,239],"pre",{"className":235,"code":236,"filename":237,"language":238,"meta":106,"style":106},"language-dart shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","import 'dart:convert';\nimport 'dart:io';\n\nvoid main() {\n  final String fileContents = _readFileSynchronously('my_file.json');\n  final Map\u003CString, dynamic> jsonData = jsonDecode(fileContents);\n\n  print('The first value is: ${jsonData.values.first}');\n}\n\nString _readFileSynchronously(String filename) {\n  final File file = File(filename);\n  final String contents = file.readAsStringSync();\n  return contents.trim();\n}\n","Example 7.1 · Example 7.1.dart","dart",[131,240,241,249,255,262,268,274,280,285,291,297,302,308,314,320,326],{"__ignoreMap":106},[242,243,246],"span",{"class":244,"line":245},"line",1,[242,247,248],{},"import 'dart:convert';\n",[242,250,252],{"class":244,"line":251},2,[242,253,254],{},"import 'dart:io';\n",[242,256,258],{"class":244,"line":257},3,[242,259,261],{"emptyLinePlaceholder":260},true,"\n",[242,263,265],{"class":244,"line":264},4,[242,266,267],{},"void main() {\n",[242,269,271],{"class":244,"line":270},5,[242,272,273],{},"  final String fileContents = _readFileSynchronously('my_file.json');\n",[242,275,277],{"class":244,"line":276},6,[242,278,279],{},"  final Map\u003CString, dynamic> jsonData = jsonDecode(fileContents);\n",[242,281,283],{"class":244,"line":282},7,[242,284,261],{"emptyLinePlaceholder":260},[242,286,288],{"class":244,"line":287},8,[242,289,290],{},"  print('The first value is: ${jsonData.values.first}');\n",[242,292,294],{"class":244,"line":293},9,[242,295,296],{},"}\n",[242,298,300],{"class":244,"line":299},10,[242,301,261],{"emptyLinePlaceholder":260},[242,303,305],{"class":244,"line":304},11,[242,306,307],{},"String _readFileSynchronously(String filename) {\n",[242,309,311],{"class":244,"line":310},12,[242,312,313],{},"  final File file = File(filename);\n",[242,315,317],{"class":244,"line":316},13,[242,318,319],{},"  final String contents = file.readAsStringSync();\n",[242,321,323],{"class":244,"line":322},14,[242,324,325],{},"  return contents.trim();\n",[242,327,329],{"class":244,"line":328},15,[242,330,296],{},[116,332,333,336,337,340,341,344],{},[220,334,335],{},"Runnable copy:"," ",[131,338,339],{},"examples\u002Fflutter_for_jobseekers_ch07\u002Fbin\u002Fread_file_sync.dart"," (reads the example's ",[131,342,343],{},"my_file.json",")",[116,346,347],{},"This code will read the contents of a JSON file, decode it, and print out the first value.",[124,349,350,372],{},[116,351,352,353,356,357,360,361,364,365,368,369,371],{},"The ",[131,354,355],{},"${jsonData.values.first}"," is using what we call ",[111,358,359],{},"String interpolation",". String interpolation will always return a string, even if the variable is null, in which case the string it returns is simply ",[220,362,363],{},"null",". That is because the variable has the ",[131,366,367],{},".toString()"," method called on it, which will usually return the string ",[220,370,363],{}," when the value is, indeed, null.",[116,373,374,375,378],{},"This can be used to get the contents of a variable, like we are doing here, but any code that's in between the braces will also be run. It is not entirely uncommon to run one or more methods within string interpolation, but do keep in mind that, if abused, this could lead to your code becoming ",[111,376,377],{},"less readable",". Anyway, let us get back to analyzing this code.",[380,381,382,383,389],"warning",{},"The problem with this block of code is that the application ",[111,384,385,386],{},"stops processing ",[220,387,388],{},"everything"," until the file has been read in, in its entirety. If you have got a very large file, this would mean the application would simply stop responding until the entire file has been read into memory. Clearly, this is not ideal.",[225,391,393],{"id":392},"asynchronous-code","Asynchronous code",[116,395,396],{},"So, let us take the same code and update it to read the file asynchronously:",[233,398,401],{"className":235,"code":399,"filename":400,"language":238,"meta":106,"style":106},"import 'dart:convert';\nimport 'dart:io';\n\nvoid main() async {\n  final String fileContents = await _readFileAsynchronously('my_file.json');\n  final Map\u003CString, dynamic> jsonData = jsonDecode(fileContents);\n\n  print('The first value is: ${jsonData.values.first}');\n}\n\nFuture\u003CString> _readFileAsynchronously(String filename) async {\n  final File file = File(filename);\n  final String contents = await file.readAsString();\n  return contents.trim();\n}\n","Example 7.2 · Example 7.2.dart",[131,402,403,407,411,415,420,425,429,433,437,441,445,450,454,459,463],{"__ignoreMap":106},[242,404,405],{"class":244,"line":245},[242,406,248],{},[242,408,409],{"class":244,"line":251},[242,410,254],{},[242,412,413],{"class":244,"line":257},[242,414,261],{"emptyLinePlaceholder":260},[242,416,417],{"class":244,"line":264},[242,418,419],{},"void main() async {\n",[242,421,422],{"class":244,"line":270},[242,423,424],{},"  final String fileContents = await _readFileAsynchronously('my_file.json');\n",[242,426,427],{"class":244,"line":276},[242,428,279],{},[242,430,431],{"class":244,"line":282},[242,432,261],{"emptyLinePlaceholder":260},[242,434,435],{"class":244,"line":287},[242,436,290],{},[242,438,439],{"class":244,"line":293},[242,440,296],{},[242,442,443],{"class":244,"line":299},[242,444,261],{"emptyLinePlaceholder":260},[242,446,447],{"class":244,"line":304},[242,448,449],{},"Future\u003CString> _readFileAsynchronously(String filename) async {\n",[242,451,452],{"class":244,"line":310},[242,453,313],{},[242,455,456],{"class":244,"line":316},[242,457,458],{},"  final String contents = await file.readAsString();\n",[242,460,461],{"class":244,"line":322},[242,462,325],{},[242,464,465],{"class":244,"line":328},[242,466,296],{},[116,468,469,336,471],{},[220,470,335],{},[131,472,473],{},"examples\u002Fflutter_for_jobseekers_ch07\u002Fbin\u002Fread_file_async.dart",[116,475,476],{},"Note the changes throughout the file:",[155,478,479,491,496],{},[158,480,352,483,486,487,490],{"icon":481,"title":482},"i-lucide-key-round","Keywords",[131,484,485],{},"async"," and ",[131,488,489],{},"await"," keywords have been added.",[158,492,495],{"icon":493,"title":494},"i-lucide-file-text","Asynchronous method","The method called on the file has been updated to use the asynchronous version.",[158,497,500,501,503],{"icon":498,"title":499},"i-lucide-hourglass","Return type","The return type of the method is now a ",[131,502,214],{},".",[116,505,506,507,509,510,512,513,515,516,518,519,522,523,525,526,529,530,533,534,536,537,540],{},"When you mark a method as ",[131,508,485],{},", it will immediately return a ",[131,511,214],{}," when invoked while using the ",[131,514,489],{}," keyword. This suspends execution of the code within the function until the ",[131,517,214],{}," that is returned by ",[131,520,521],{},"readAsString()"," completes. Once ",[131,524,521],{}," has finished its work, the function will resume processing until it encounters a ",[131,527,528],{},"return"," statement, which completes the function by returning a ",[131,531,532],{},"Future\u003CString>",", where the string is the value returned from the ",[131,535,521],{}," method. This allows the application to ",[111,538,539],{},"continue processing"," even as the file is being read into memory.",[225,542,544,545],{"id":543},"using-then","Using ",[131,546,547],{},".then()",[116,549,550],{},"Let us look at another example. Consider the following code:",[233,552,555],{"className":235,"code":553,"filename":554,"language":238,"meta":106,"style":106},"String? hello = await Future\u003CString?>.delayed(const Duration(seconds: 1)).then((_) => \"Hello\");\n","Example 7.3 · Example 7.3.dart",[131,556,557],{"__ignoreMap":106},[242,558,559],{"class":244,"line":245},[242,560,553],{},[116,562,563,564,566,567,570,571,574,575,578,579,581,582,584,585,587,588,590,591,593,594,596,597,600,601,603,604,606],{},"In this example, we have foregone calling a function that is explicitly marked as ",[131,565,485],{},", but we are still waiting for a return value. Can you see how the variable ",[131,568,569],{},"hello"," will only be set to the string ",[131,572,573],{},"\"Hello\""," after the ",[131,576,577],{},"Future.delayed()"," completes? We are calling a ",[131,580,214],{}," directly and using the ",[131,583,547],{}," method to get the value. This code will be synchronous, meaning that the rest of the application will pause execution until it completes the ",[131,586,214],{},". This contrasts with the way we called a function marked with the ",[131,589,485],{}," keyword before. The ",[131,592,485],{},"\u002F",[131,595,489],{}," method ",[220,598,599],{},"simulates"," the ",[131,602,547],{}," way of completing the ",[131,605,214],{}," but is not blocking.",[124,608,609,612,613,615,616,618,619,621,622,624,625,627,628,630,631,634],{},[111,610,611],{},"Corrected:"," this line does not block the application either. It uses ",[131,614,489],{},", so it can only run inside an ",[131,617,485],{}," function, and ",[131,620,489],{}," behaves the same whether the ",[131,623,214],{}," comes from an ",[131,626,485],{}," function or from ",[131,629,547],{},": \"The await expression makes execution pause until that object is available\" (Dart, n.d.-a). Only the enclosing function pauses; the event loop keeps running other code, such as event handlers (Dart, n.d.-b). Code blocks only when it calls a synchronous API, such as ",[131,632,633],{},"readAsStringSync()"," above.",[116,636,637,638,640,641,643,644,646,647,649,650,653,654,656],{},"Also in this example, the ",[131,639,214],{}," is obvious as a ",[131,642,214],{}," and it will only take a single second for that ",[131,645,214],{}," to complete. Not all futures will always appear to be of the type ",[131,648,214],{}," (although we now know better, since we realize that ",[220,651,652],{},"all"," future functions return a ",[131,655,214],{}," and can, once the future has been resolved, be cast into the eventual final type we are expecting), nor will most futures complete in such a specific amount of time.",[658,659,660,661,664,665,667],"tip",{},"Think back to the first example, where we read in the contents of the file. The ",[131,662,663],{},"file.readAsString()"," method returned a ",[131,666,214],{},", but it certainly did not explicitly say so, nor did we have any control over how long the process would take to complete.",[116,669,670,671,593,673,675],{},"Now that we have a basic understanding of asynchronous operations, futures, and the ",[131,672,485],{},[131,674,489],{}," keywords, let us look at the http package and learn how to use it to make an API call.",[150,677,171],{"id":678},"requesting-data-from-an-api",[124,680,681,682,685],{},"An API works a lot like ",[111,683,684],{},"ordering food from a restaurant",". You give your order to a waiter, who disappears for a little while before bringing the food you ordered back to your table. You have no idea how long it will take to get that food, who cooked it or how it was prepared, or where any of the ingredients came from. APIs are, in this regard, very similar. You approach the API with a request, then wait for a response, and you have no idea what happened on the server between those two actions.",[116,687,688,689,692,693,696,697,699,700,702,703,486,706,503],{},"To communicate with the API, we will first need an ",[111,690,691],{},"HTTP client"," which will do the communication on our behalf. The ",[131,694,695],{},"http"," package will suffice, but there are other, more powerful HTTP clients (such as Dio) out there if you find yourself running into the limitations of ",[131,698,695],{},". The ",[131,701,695],{}," package comes with a multitude of classes, and the two we are going to focus on right now are ",[131,704,705],{},"Client",[131,707,708],{},"Response",[116,710,711,713,714,716],{},[131,712,705],{}," is the HTTP client itself, used to communicate with APIs or make other network calls, such as downloading a file. There are a couple of different ways that people invoke a ",[131,715,705],{}," object.",[225,718,720],{"id":719},"importing-with-a-namespace","Importing with a namespace",[116,722,723],{},"Some will import the http package using a namespace like http, which looks like the following:",[233,725,728],{"className":235,"code":726,"filename":727,"language":238,"meta":106,"style":106},"import 'package:http\u002Fhttp.dart' as http;\n","Example 7.4 · Example 7.4.dart",[131,729,730],{"__ignoreMap":106},[242,731,732],{"class":244,"line":245},[242,733,726],{},[116,735,736,737,740,741,743,744,747,748,751],{},"Then, they use it to make requests with ",[131,738,739],{},"http.get(…)",". This is a valid way of working with the package, but it does mean that every class that the ",[131,742,695],{}," package provides will need to be referenced by a preceding ",[131,745,746],{},"http.",", such as ",[131,749,750],{},"http.Response",". This can make the code a bit harder to work with in some instances. However, there are some advantages to using namespaces, as well:",[753,754,755,761,765,769,773],"field-group",{},[756,757,760],"field",{"name":758,"type":759},"Avoid naming conflicts","advantage","By using a namespace, you can avoid naming conflicts between different packages or libraries that you are importing. Since each package has its own namespace, you can use the same variable or class name from different packages without any conflicts.",[756,762,764],{"name":763,"type":759},"Better readability","When you use a namespace, it makes your code more readable and organized. It allows you to group related classes, functions, and constants together under a common name, which makes it easier to understand the purpose of each import.",[756,766,768],{"name":767,"type":759},"Faster compilation","When you use a namespace, it can improve the compilation time of your code. This is because the compiler can optimize the imports by only including the necessary code from each package, instead of importing the entire package.",[756,770,772],{"name":771,"type":759},"Easier code maintenance","Using a namespace can make it easier to maintain your code in the long run. If you need to update a package, you can simply update the import statement without having to change all the references to that package throughout your codebase.",[756,774,776],{"name":775,"type":759},"Better code reusability","When you use a namespace, it allows you to reuse the same code across different projects without any conflicts. Since each package has its own namespace, it can be easily included in different projects without any issues.",[124,778,779,781],{},[111,780,611],{}," a prefix does not make compilation faster. The Dart documentation gives one reason for it: \"If you import two libraries that have conflicting identifiers, then you can specify a prefix for one or both libraries\" (Dart, n.d.-c), and it says nothing about compilation time. A prefixed import brings in the same library as an unprefixed one; only the way you refer to its names changes.",[225,783,785,786,788],{"id":784},"instantiating-a-client-per-call","Instantiating a ",[131,787,705],{}," per call",[116,790,791,792,794,795,798],{},"Another way is by simply importing ",[131,793,695],{},", then using ",[131,796,797],{},"Client().get(…)",", where you instantiate the class and use it in the same line. No longer would you have to prefix each of the classes with the library name, however this can also become confusing.",[380,800,801,802,804,805,503],{},"If you are authenticating to the API by using HTTP headers, you will have to add those headers each time you make a request, instead of having a single HTTP client which remembers those headers. Not to mention the fact that instantiating a new ",[131,803,705],{}," for each call is ",[111,806,807],{},"horribly inefficient",[225,809,811],{"id":810},"the-recommended-hybrid","The recommended hybrid",[658,813,814,815,818,819,821],{},"For these reasons, it is recommended to go with a ",[111,816,817],{},"hybrid solution",": import http normally, then create a ",[131,820,705],{}," variable, which you can use to make your HTTP calls.",[116,823,824],{},"Let us put this into action with a quick example:",[233,826,829],{"className":235,"code":827,"filename":828,"language":238,"meta":106,"style":106},"import 'dart:convert';\nimport 'package:http\u002Fhttp.dart';\n\nClient http = Client();\n\nvoid main() async {\n  final Response response = await fetchAlbum();\n  if (response.statusCode == 200) {\n    final data = jsonDecode(response.body);\n    print('The album title is: ${data[\"title\"]}');\n  }\n}\n\nFuture\u003CResponse> fetchAlbum() {\n  return http.get(Uri.parse('https:\u002F\u002Fjsonplaceholder.typicode.com\u002Falbums\u002F1'));\n}\n","Example 7.5 · Example 7.5.dart",[131,830,831,835,840,844,849,853,857,862,867,872,877,882,886,890,895,900],{"__ignoreMap":106},[242,832,833],{"class":244,"line":245},[242,834,248],{},[242,836,837],{"class":244,"line":251},[242,838,839],{},"import 'package:http\u002Fhttp.dart';\n",[242,841,842],{"class":244,"line":257},[242,843,261],{"emptyLinePlaceholder":260},[242,845,846],{"class":244,"line":264},[242,847,848],{},"Client http = Client();\n",[242,850,851],{"class":244,"line":270},[242,852,261],{"emptyLinePlaceholder":260},[242,854,855],{"class":244,"line":276},[242,856,419],{},[242,858,859],{"class":244,"line":282},[242,860,861],{},"  final Response response = await fetchAlbum();\n",[242,863,864],{"class":244,"line":287},[242,865,866],{},"  if (response.statusCode == 200) {\n",[242,868,869],{"class":244,"line":293},[242,870,871],{},"    final data = jsonDecode(response.body);\n",[242,873,874],{"class":244,"line":299},[242,875,876],{},"    print('The album title is: ${data[\"title\"]}');\n",[242,878,879],{"class":244,"line":304},[242,880,881],{},"  }\n",[242,883,884],{"class":244,"line":310},[242,885,296],{},[242,887,888],{"class":244,"line":316},[242,889,261],{"emptyLinePlaceholder":260},[242,891,892],{"class":244,"line":322},[242,893,894],{},"Future\u003CResponse> fetchAlbum() {\n",[242,896,897],{"class":244,"line":328},[242,898,899],{},"  return http.get(Uri.parse('https:\u002F\u002Fjsonplaceholder.typicode.com\u002Falbums\u002F1'));\n",[242,901,903],{"class":244,"line":902},16,[242,904,296],{},[116,906,907,908,911],{},"This code will fetch some data from an API, then print out a piece of it. There is a lot of room for improvement, though. For starters, it only checks for an HTTP status code of ",[131,909,910],{},"200 (OK)"," and has no error handling whatsoever.",[225,913,915],{"id":914},"guard-clauses","Guard clauses",[116,917,918],{},"Instead of checking explicitly for a success state, we should instead check for the opposite:",[233,920,923],{"className":235,"code":921,"filename":922,"language":238,"meta":106,"style":106},"void main() async {\n  final Response response = await fetchAlbum();\n  if (response.statusCode != 200) {\n    print('There was an error fetching the album.');\n    return;\n  }\n\n  final data = jsonDecode(response.body);\n  print('The album title is: ${data[\"title\"]}');\n}\n\nClient http = Client();\n\nFuture\u003CResponse> fetchAlbum() {\n  return http.get(Uri.parse('https:\u002F\u002Fjsonplaceholder.typicode.com\u002Falbums\u002Fa'));\n}\n","Example 7.6 · Example 7.6.dart",[131,924,925,929,933,938,943,948,952,956,961,966,970,974,978,982,986,991],{"__ignoreMap":106},[242,926,927],{"class":244,"line":245},[242,928,419],{},[242,930,931],{"class":244,"line":251},[242,932,861],{},[242,934,935],{"class":244,"line":257},[242,936,937],{},"  if (response.statusCode != 200) {\n",[242,939,940],{"class":244,"line":264},[242,941,942],{},"    print('There was an error fetching the album.');\n",[242,944,945],{"class":244,"line":270},[242,946,947],{},"    return;\n",[242,949,950],{"class":244,"line":276},[242,951,881],{},[242,953,954],{"class":244,"line":282},[242,955,261],{"emptyLinePlaceholder":260},[242,957,958],{"class":244,"line":287},[242,959,960],{},"  final data = jsonDecode(response.body);\n",[242,962,963],{"class":244,"line":293},[242,964,965],{},"  print('The album title is: ${data[\"title\"]}');\n",[242,967,968],{"class":244,"line":299},[242,969,296],{},[242,971,972],{"class":244,"line":304},[242,973,261],{"emptyLinePlaceholder":260},[242,975,976],{"class":244,"line":310},[242,977,848],{},[242,979,980],{"class":244,"line":316},[242,981,261],{"emptyLinePlaceholder":260},[242,983,984],{"class":244,"line":322},[242,985,894],{},[242,987,988],{"class":244,"line":328},[242,989,990],{},"  return http.get(Uri.parse('https:\u002F\u002Fjsonplaceholder.typicode.com\u002Falbums\u002Fa'));\n",[242,992,993],{"class":244,"line":902},[242,994,296],{},[116,996,997,998,1001,1002,1004,1005,1007],{},"Note how the URI for the album to fetch has changed from a ",[131,999,1000],{},"1"," to an ",[131,1003,140],{}," causing the API call to return a non-200 status code. We now check for that non-200 status code in the same way we were checking for a successful response before. If we receive any status code other than a 200, we print out our error message and then prevent the rest of the function from executing with the ",[131,1006,528],{}," statement.",[658,1009,1010,1011,1014,1015,1018],{},"By doing it this way, we are using a ",[111,1012,1013],{},"guard clause"," style of programming. That is, we know what code we want to run if everything goes well, so let us just check for all the things which could go wrong ",[220,1016,1017],{},"before"," running the happy-path code. This will reduce the number of errors we have to check for later in our code.",[150,1020,174],{"id":1021},"classifying-api-responses",[116,1023,1024,1025,1028],{},"In our previous example, we referenced the data returned from the API when printing it out using ",[131,1026,1027],{},"data[\"title\"]",". This is not very robust, however. If the element lacks a title field, the code will break. Additionally, it does not give us a nice Dart object to work with. It is time we create our own class to handle this, but first we need to understand the structure of the data we are working with.",[225,1030,1032],{"id":1031},"the-structure-of-json","The structure of JSON",[116,1034,1035,1036,1039],{},"By printing out the ",[131,1037,1038],{},"response.body",", we can see the JSON which is returned to us:",[233,1041,1046],{"className":1042,"code":1044,"language":1045,"meta":106},[1043],"language-text","{userId: 1, id: 1, title: quidem molestiae enim}\n","text",[131,1047,1044],{"__ignoreMap":106},[116,1049,1050,1053,1054,1057],{},[111,1051,1052],{},"JavaScript Object Notation (JSON)"," is a structured key-value way of representing data. Note that when we ask Dart to print out the JSON for us, it has stripped away the quotes which would normally be around any strings. A ",[220,1055,1056],{},"raw"," JSON response would look like the following:",[233,1059,1063],{"className":1060,"code":1061,"language":1062,"meta":106,"style":106},"language-json shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","{\"userId\": 1, \"id\": 1, title: \"quidem molestiae enim\"}\n","json",[131,1064,1065],{"__ignoreMap":106},[242,1066,1067,1071,1074,1078,1080,1083,1087,1090,1093,1096,1098,1100,1102,1104,1108,1110,1112,1116,1118],{"class":244,"line":245},[242,1068,1070],{"class":1069},"sMK4o","{",[242,1072,1073],{"class":1069},"\"",[242,1075,1077],{"class":1076},"spNyl","userId",[242,1079,1073],{"class":1069},[242,1081,1082],{"class":1069},":",[242,1084,1086],{"class":1085},"sbssI"," 1",[242,1088,1089],{"class":1069},",",[242,1091,1092],{"class":1069}," \"",[242,1094,1095],{"class":1076},"id",[242,1097,1073],{"class":1069},[242,1099,1082],{"class":1069},[242,1101,1086],{"class":1085},[242,1103,1089],{"class":1069},[242,1105,1107],{"class":1106},"sTEyZ"," title",[242,1109,1082],{"class":1069},[242,1111,1092],{"class":1069},[242,1113,1115],{"class":1114},"sfazB","quidem molestiae enim",[242,1117,1073],{"class":1069},[242,1119,296],{"class":1069},[124,1121,1122,1124,1125,486,1128,1131,1132,503],{},[111,1123,611],{}," this line is not valid JSON. In JSON every key is a double-quoted string, as ",[131,1126,1127],{},"\"userId\"",[131,1129,1130],{},"\"id\""," are, so the raw response reads ",[131,1133,1134],{},"\"title\": \"quidem molestiae enim\"",[380,1136,1137,1138,1141,1142,1145,1146,1149],{},"Understanding the difference in how the data can be displayed is vital. See how in both examples the integers are unquoted? They are a different data type (",[131,1139,1140],{},"int",") than the other value (a ",[131,1143,1144],{},"String","). Trying to access the integer as a string will ",[111,1147,1148],{},"throw an error"," in your code, thus why it is important to know how the JSON data is structured.",[225,1151,352,1153,1156],{"id":1152},"the-album-class",[131,1154,1155],{},"Album"," class",[116,1158,1159],{},"Alright, now that we understand the structure of the data in the JSON response, we can create a class that we will eventually use to cast the response data into.",[233,1161,1164],{"className":235,"code":1162,"filename":1163,"language":238,"meta":106,"style":106},"class Album {\n  int? userId;\n  int? id;\n  String? title;\n\n  Album({this.userId, this.id, this.title});\n\n  factory Album.fromJson(Map\u003CString, dynamic> json) => Album(\n    userId: json[\"userId\"],\n    id: json[\"id\"],\n    title: json[\"title\"],\n  );\n}\n","Example 7.7 · Example 7.7.dart",[131,1165,1166,1171,1176,1181,1186,1190,1195,1199,1204,1209,1214,1219,1224],{"__ignoreMap":106},[242,1167,1168],{"class":244,"line":245},[242,1169,1170],{},"class Album {\n",[242,1172,1173],{"class":244,"line":251},[242,1174,1175],{},"  int? userId;\n",[242,1177,1178],{"class":244,"line":257},[242,1179,1180],{},"  int? id;\n",[242,1182,1183],{"class":244,"line":264},[242,1184,1185],{},"  String? title;\n",[242,1187,1188],{"class":244,"line":270},[242,1189,261],{"emptyLinePlaceholder":260},[242,1191,1192],{"class":244,"line":276},[242,1193,1194],{},"  Album({this.userId, this.id, this.title});\n",[242,1196,1197],{"class":244,"line":282},[242,1198,261],{"emptyLinePlaceholder":260},[242,1200,1201],{"class":244,"line":287},[242,1202,1203],{},"  factory Album.fromJson(Map\u003CString, dynamic> json) => Album(\n",[242,1205,1206],{"class":244,"line":293},[242,1207,1208],{},"    userId: json[\"userId\"],\n",[242,1210,1211],{"class":244,"line":299},[242,1212,1213],{},"    id: json[\"id\"],\n",[242,1215,1216],{"class":244,"line":304},[242,1217,1218],{},"    title: json[\"title\"],\n",[242,1220,1221],{"class":244,"line":310},[242,1222,1223],{},"  );\n",[242,1225,1226],{"class":244,"line":316},[242,1227,296],{},[116,1229,1230,336,1232],{},[220,1231,335],{},[131,1233,1234],{},"examples\u002Fflutter_for_jobseekers_ch07\u002Flib\u002Falbum.dart",[116,1236,1237,1238,1241,1242,1245,1246,1248,1249,1251,1252,1255,1256,1258,1259,1261],{},"We have made all the different fields ",[111,1239,1240],{},"nullable",", since we cannot guarantee the response of the API, and we have no idea if all the fields will be returned. We also created a ",[131,1243,1244],{},"factory"," that we can call when creating our new ",[131,1247,1155],{}," class. The ",[131,1250,1244],{}," will take in the JSON data (as a ",[131,1253,1254],{},"Map\u003CString, dynamic>",") and return the ",[131,1257,1155],{}," object. See how we were able to map the JSON values to the corresponding ",[131,1260,1155],{}," parameters?",[225,1263,1265,1266],{"id":1264},"returning-an-album","Returning an ",[131,1267,1155],{},[116,1269,1270,1271,1274,1275,1277,1278,1280,1281,1284],{},"Now that we have a class with parameters matching the JSON response, we can update our code to use it. First, let us fix the ",[131,1272,1273],{},"fetchAlbum"," method. Instead of returning a ",[131,1276,708],{},", we should return our ",[131,1279,1155],{},". After all, it is not a ",[131,1282,1283],{},"fetchResponse"," method!",[233,1286,1289],{"className":235,"code":1287,"filename":1288,"language":238,"meta":106,"style":106},"Future\u003CAlbum?> fetchAlbum() async {\n  final Response response = await http.get(Uri.parse('https:\u002F\u002Fjsonplaceholder.typicode.com\u002Falbums\u002F1'));\n\n  if (response.statusCode != 200) {\n    print('There was an error fetching the album.');\n    return null;\n  }\n\n  final Map\u003CString, dynamic> data = jsonDecode(response.body);\n  final Album album = Album.fromJson(data);\n\n  return album;\n}\n","Example 7.8 · Example 7.8.dart",[131,1290,1291,1296,1301,1305,1309,1313,1318,1322,1326,1331,1336,1340,1345],{"__ignoreMap":106},[242,1292,1293],{"class":244,"line":245},[242,1294,1295],{},"Future\u003CAlbum?> fetchAlbum() async {\n",[242,1297,1298],{"class":244,"line":251},[242,1299,1300],{},"  final Response response = await http.get(Uri.parse('https:\u002F\u002Fjsonplaceholder.typicode.com\u002Falbums\u002F1'));\n",[242,1302,1303],{"class":244,"line":257},[242,1304,261],{"emptyLinePlaceholder":260},[242,1306,1307],{"class":244,"line":264},[242,1308,937],{},[242,1310,1311],{"class":244,"line":270},[242,1312,942],{},[242,1314,1315],{"class":244,"line":276},[242,1316,1317],{},"    return null;\n",[242,1319,1320],{"class":244,"line":282},[242,1321,881],{},[242,1323,1324],{"class":244,"line":287},[242,1325,261],{"emptyLinePlaceholder":260},[242,1327,1328],{"class":244,"line":293},[242,1329,1330],{},"  final Map\u003CString, dynamic> data = jsonDecode(response.body);\n",[242,1332,1333],{"class":244,"line":299},[242,1334,1335],{},"  final Album album = Album.fromJson(data);\n",[242,1337,1338],{"class":244,"line":304},[242,1339,261],{"emptyLinePlaceholder":260},[242,1341,1342],{"class":244,"line":310},[242,1343,1344],{},"  return album;\n",[242,1346,1347],{"class":244,"line":316},[242,1348,296],{},[116,1350,1351,1352,1354,1355,1357,1358,1361],{},"This is already much better. We moved the error handling from the main method into the ",[131,1353,1273],{}," method, too. It makes more sense for that logic to be here, rather than in the main method, because this is where the HTTP call is being made and where the logic deciding whether to cast the resulting JSON into an ",[131,1356,1155],{}," is stored. This results in a much cleaner ",[131,1359,1360],{},"main"," method, by comparison:",[233,1363,1366],{"className":235,"code":1364,"filename":1365,"language":238,"meta":106,"style":106},"void main() async {\n  final Album? album = await fetchAlbum();\n  print('The album title is: ${album?.title ?? \"Undefined\"}');\n}\n","Example 7.9 · Example 7.9.dart",[131,1367,1368,1372,1377,1382],{"__ignoreMap":106},[242,1369,1370],{"class":244,"line":245},[242,1371,419],{},[242,1373,1374],{"class":244,"line":251},[242,1375,1376],{},"  final Album? album = await fetchAlbum();\n",[242,1378,1379],{"class":244,"line":257},[242,1380,1381],{},"  print('The album title is: ${album?.title ?? \"Undefined\"}');\n",[242,1383,1384],{"class":244,"line":264},[242,1385,296],{},[116,1387,1388,1389,1391,1392,1395,1396,1398],{},"Also, take note of the line where we turn the data into an ",[131,1390,1155],{}," using our new ",[131,1393,1394],{},"fromJson"," method. If we wanted, we could check some parameters in the ",[131,1397,1155],{}," before returning it, such as making sure the title is not null. In this instance, we are just going to return whatever we end up with.",[658,1400,1401,1402,138,1405,1407,1408,1410,1411,1414,1415,1418],{},"Another way of handling this would be to specify ",[111,1403,1404],{},"defaults",[131,1406,1155],{},"'s ",[131,1409,1394],{}," method. For example, you could use ",[131,1412,1413],{},"json[\"title\"] ?? \"Undefined\""," and mark the ",[131,1416,1417],{},"title"," as no longer nullable. That might not work in every situation, though, so instead we are simply checking for the title when we print it out and handling the error state there. There is one other method we can use to handle errors like this, which we will look at in the next section.",[225,1420,1422],{"id":1421},"passing-in-the-id","Passing in the ID",[116,1424,1425,1426,1428,1429,1431,1432,1435],{},"There is one more thing we can do to improve the ",[131,1427,1273],{}," method, though. Since we know that the API references a given album by its integer ID, we can pass in that integer ID to the ",[131,1430,1273],{}," method when we call it. This would result in a much more ",[111,1433,1434],{},"reusable"," piece of code, instead of the method only ever returning a single, given album. Here is what that might look like:",[233,1437,1440],{"className":235,"code":1438,"filename":1439,"language":238,"meta":106,"style":106},"void main() async {\n  final Album? album = await fetchAlbum(id: 1);\n  print('The album title is: ${album?.title ?? \"Undefined\"}');\n}\n\nFuture\u003CAlbum?> fetchAlbum({required int id}) async {\n  final Response response = await http.get(Uri.parse('https:\u002F\u002Fjsonplaceholder.typicode.com\u002Falbums\u002F$id'));\n\n  if (response.statusCode != 200) {\n    print('There was an error fetching the album.');\n    return null;\n  }\n\n  final Map\u003CString, dynamic> data = jsonDecode(response.body);\n  final Album album = Album.fromJson(data);\n\n  return album;\n}\n","Example 7.10 · Example 7.10.dart",[131,1441,1442,1446,1451,1455,1459,1463,1468,1473,1477,1481,1485,1489,1493,1497,1501,1505,1509,1514],{"__ignoreMap":106},[242,1443,1444],{"class":244,"line":245},[242,1445,419],{},[242,1447,1448],{"class":244,"line":251},[242,1449,1450],{},"  final Album? album = await fetchAlbum(id: 1);\n",[242,1452,1453],{"class":244,"line":257},[242,1454,1381],{},[242,1456,1457],{"class":244,"line":264},[242,1458,296],{},[242,1460,1461],{"class":244,"line":270},[242,1462,261],{"emptyLinePlaceholder":260},[242,1464,1465],{"class":244,"line":276},[242,1466,1467],{},"Future\u003CAlbum?> fetchAlbum({required int id}) async {\n",[242,1469,1470],{"class":244,"line":282},[242,1471,1472],{},"  final Response response = await http.get(Uri.parse('https:\u002F\u002Fjsonplaceholder.typicode.com\u002Falbums\u002F$id'));\n",[242,1474,1475],{"class":244,"line":287},[242,1476,261],{"emptyLinePlaceholder":260},[242,1478,1479],{"class":244,"line":293},[242,1480,937],{},[242,1482,1483],{"class":244,"line":299},[242,1484,942],{},[242,1486,1487],{"class":244,"line":304},[242,1488,1317],{},[242,1490,1491],{"class":244,"line":310},[242,1492,881],{},[242,1494,1495],{"class":244,"line":316},[242,1496,261],{"emptyLinePlaceholder":260},[242,1498,1499],{"class":244,"line":322},[242,1500,1330],{},[242,1502,1503],{"class":244,"line":328},[242,1504,1335],{},[242,1506,1507],{"class":244,"line":902},[242,1508,261],{"emptyLinePlaceholder":260},[242,1510,1512],{"class":244,"line":1511},17,[242,1513,1344],{},[242,1515,1517],{"class":244,"line":1516},18,[242,1518,296],{},[116,1520,1521,336,1523,1526,1527,1529],{},[220,1522,335],{},[131,1524,1525],{},"examples\u002Fflutter_for_jobseekers_ch07\u002Flib\u002Falbum_api.dart"," (",[131,1528,1273],{},", shared by the scripts and the app)",[116,1531,1532],{},"Now that we are passing in an ID to the method, we can leverage this flexibility to grab a whole bunch of albums:",[233,1534,1537],{"className":235,"code":1535,"filename":1536,"language":238,"meta":106,"style":106},"void main() async {\n  final List\u003Cint> albumsToFetch = [1, 2, 3];\n  for (final int albumToFetch in albumsToFetch) {\n    final Album? album = await fetchAlbum(id: albumToFetch);\n    print(\"Album ${album?.id}'s title is: ${album?.title ?? 'Undefined'}\");\n  }\n}\n","Example 7.11 · Example 7.11.dart",[131,1538,1539,1543,1548,1553,1558,1563,1567],{"__ignoreMap":106},[242,1540,1541],{"class":244,"line":245},[242,1542,419],{},[242,1544,1545],{"class":244,"line":251},[242,1546,1547],{},"  final List\u003Cint> albumsToFetch = [1, 2, 3];\n",[242,1549,1550],{"class":244,"line":257},[242,1551,1552],{},"  for (final int albumToFetch in albumsToFetch) {\n",[242,1554,1555],{"class":244,"line":264},[242,1556,1557],{},"    final Album? album = await fetchAlbum(id: albumToFetch);\n",[242,1559,1560],{"class":244,"line":270},[242,1561,1562],{},"    print(\"Album ${album?.id}'s title is: ${album?.title ?? 'Undefined'}\");\n",[242,1564,1565],{"class":244,"line":276},[242,1566,881],{},[242,1568,1569],{"class":244,"line":282},[242,1570,296],{},[116,1572,1573,336,1575],{},[220,1574,335],{},[131,1576,1577],{},"examples\u002Fflutter_for_jobseekers_ch07\u002Fbin\u002Ffetch_albums.dart",[150,1579,177],{"id":1580},"sending-data-to-an-api",[116,1582,1583,1584,1587],{},"Now that we know how to receive data from an API, it is time to talk about sending data to an API. We will expand our previous example to include a ",[131,1585,1586],{},"createAlbum"," method that we can call. This method will tell the API to create an album with a given title:",[233,1589,1592],{"className":235,"code":1590,"filename":1591,"language":238,"meta":106,"style":106},"Future\u003CAlbum> createAlbum(String title) async {\n  final response = await http.post(\n    Uri.parse('https:\u002F\u002Fjsonplaceholder.typicode.com\u002Falbums'),\n    headers: \u003CString, String>{\n      'Content-Type': 'application\u002Fjson; charset=UTF-8',\n    },\n    body: jsonEncode(\u003CString, String>{\n        'title': title,\n    }),\n  );\n\n  if (response.statusCode != 201) {\n    throw Exception('Failed to create album.');\n  }\n\n  return Album.fromJson(jsonDecode(response.body));\n}\n","Example 7.12 · Example 7.12.dart",[131,1593,1594,1599,1604,1609,1614,1619,1624,1629,1634,1639,1643,1647,1652,1657,1661,1665,1670],{"__ignoreMap":106},[242,1595,1596],{"class":244,"line":245},[242,1597,1598],{},"Future\u003CAlbum> createAlbum(String title) async {\n",[242,1600,1601],{"class":244,"line":251},[242,1602,1603],{},"  final response = await http.post(\n",[242,1605,1606],{"class":244,"line":257},[242,1607,1608],{},"    Uri.parse('https:\u002F\u002Fjsonplaceholder.typicode.com\u002Falbums'),\n",[242,1610,1611],{"class":244,"line":264},[242,1612,1613],{},"    headers: \u003CString, String>{\n",[242,1615,1616],{"class":244,"line":270},[242,1617,1618],{},"      'Content-Type': 'application\u002Fjson; charset=UTF-8',\n",[242,1620,1621],{"class":244,"line":276},[242,1622,1623],{},"    },\n",[242,1625,1626],{"class":244,"line":282},[242,1627,1628],{},"    body: jsonEncode(\u003CString, String>{\n",[242,1630,1631],{"class":244,"line":287},[242,1632,1633],{},"        'title': title,\n",[242,1635,1636],{"class":244,"line":293},[242,1637,1638],{},"    }),\n",[242,1640,1641],{"class":244,"line":299},[242,1642,1223],{},[242,1644,1645],{"class":244,"line":304},[242,1646,261],{"emptyLinePlaceholder":260},[242,1648,1649],{"class":244,"line":310},[242,1650,1651],{},"  if (response.statusCode != 201) {\n",[242,1653,1654],{"class":244,"line":316},[242,1655,1656],{},"    throw Exception('Failed to create album.');\n",[242,1658,1659],{"class":244,"line":322},[242,1660,881],{},[242,1662,1663],{"class":244,"line":328},[242,1664,261],{"emptyLinePlaceholder":260},[242,1666,1667],{"class":244,"line":902},[242,1668,1669],{},"  return Album.fromJson(jsonDecode(response.body));\n",[242,1671,1672],{"class":244,"line":1511},[242,1673,296],{},[116,1675,1676,336,1678,1526,1680,344],{},[220,1677,335],{},[131,1679,1525],{},[131,1681,1586],{},[116,1683,1684,1685,1688,1689,1692,1693,1696,1697,1082],{},"Here, we are using ",[131,1686,1687],{},"http.post"," rather than ",[131,1690,1691],{},"http.get"," to send data to the API. The structure is a bit different when we are doing a ",[131,1694,1695],{},"POST"," instead of a ",[131,1698,1699],{},"GET",[1701,1702,1704,1709,1712,1716,1742,1746],"steps",{"level":1703},"4",[1705,1706,1708],"h4",{"id":1707},"headers","Headers",[116,1710,1711],{},"We need to specify the headers, so the API knows what type of data we are sending over. In this instance, we tell it that we are sending JSON data that has been encoded using UTF-8.",[1705,1713,1715],{"id":1714},"body","Body",[116,1717,1718,1719,1722,1723,1726,1727,1730,1731,1734,1735,1737,1738,1741],{},"We must specify a body of the HTTP request that includes a JSON object with the data we are sending to the API. The body starts out as a ",[131,1720,1721],{},"Map",", and then we use ",[131,1724,1725],{},"jsonEncode"," from the ",[131,1728,1729],{},"dart:convert"," package to convert it to a JSON string. The ",[131,1732,1733],{},"\u003CString, String>"," tells us what type of data the ",[131,1736,1721],{}," has for its keys and values. If you were sending a mix of strings, integers, lists, and so on, you would use ",[131,1739,1740],{},"\u003CString, dynamic>",", instead.",[1705,1743,1745],{"id":1744},"uri","URI",[116,1747,1748,1749,1751],{},"Once the data has been converted to a JSON object, the last thing which differs is the URI to which we send the request. If you recall, in the previous example, our HTTP ",[131,1750,1699],{}," specified an ID for the album. In this instance, since we are creating a new album, we have not been assigned an ID yet. For this reason, we omit the ID from the URI, and once the API request has been completed, the resulting data which is returned will have our brand-new ID.",[380,1753,1754,1755,1758,1759,1758,1762,1765,1766,1769,1770,503],{},"Not all APIs are made equal, and it is important to note that. This method of creating and retrieving data is common, but far from the only way for APIs to be designed. It is important to check with the documentation of the API that you are working with to understand what URIs to use for the different ",[111,1756,1757],{},"create",", ",[111,1760,1761],{},"read",[111,1763,1764],{},"update",", and ",[111,1767,1768],{},"delete"," (CRUD) operations and what format the data should be sent in. To read the documentation of the example API we have been using, head over to ",[111,1771,1772],{},[140,1773,1774],{"href":1774,"rel":1775},"https:\u002F\u002Fjsonplaceholder.typicode.com\u002F",[144],[116,1777,1778],{},"To run our code and create an album, it is as simple as the following:",[233,1780,1783],{"className":235,"code":1781,"filename":1782,"language":238,"meta":106,"style":106},"void main() async {\n  final Album album = await createAlbum(\"My own album\");\n  print(\"Album ${album.id}'s title is: ${album.title}\");\n}\n","Example 7.13 · Example 7.13.dart",[131,1784,1785,1789,1794,1799],{"__ignoreMap":106},[242,1786,1787],{"class":244,"line":245},[242,1788,419],{},[242,1790,1791],{"class":244,"line":251},[242,1792,1793],{},"  final Album album = await createAlbum(\"My own album\");\n",[242,1795,1796],{"class":244,"line":257},[242,1797,1798],{},"  print(\"Album ${album.id}'s title is: ${album.title}\");\n",[242,1800,1801],{"class":244,"line":264},[242,1802,296],{},[124,1804,1805,1806,1809,1810],{},"JSONPlaceholder only pretends to store the album: \"The resource is not really created on the server, but the response is faked as if\" (JSONPlaceholder, n.d.). Every new album comes back with the ID ",[131,1807,1808],{},"101",", and fetching that ID afterwards finds nothing. ",[220,1811,1812],{},"(Added, not in the book.)",[225,1814,1816],{"id":1815},"throwing-instead-of-returning-null","Throwing instead of returning null",[116,1818,1819,1820,1822,1823,1825,1826,1828,1829,1832,1833,1836,1837,1840],{},"Look back at the ",[131,1821,1586],{}," method for a minute. Remember how we handled error cases in the ",[131,1824,1273],{}," method? The ",[131,1827,1586],{}," method uses a different way of handling those error cases. This time, we are checking for the HTTP status code ",[131,1830,1831],{},"201 (Created)"," and in the instance that we do not receive a successful response, we ",[131,1834,1835],{},"throw"," an ",[131,1838,1839],{},"Exception",", rather than making our return value nullable.",[658,1842,1843,1844,1847],{},"This is an ideal way to handle errors, as we can use other ",[111,1845,1846],{},"built-in logic to catch"," those errors.",[233,1849,1852],{"className":235,"code":1850,"filename":1851,"language":238,"meta":106,"style":106},"void main() async {\n  try {\n    final Album album = await createAlbum(\"My own album\");\n    print(\"Album ${album.id}'s title is: ${album.title}\");\n  } catch (e) {\n    print(\"There was an exception: $e\");\n  }\n}\n","Example 7.14 · Example 7.14.dart",[131,1853,1854,1858,1863,1868,1873,1878,1883,1887],{"__ignoreMap":106},[242,1855,1856],{"class":244,"line":245},[242,1857,419],{},[242,1859,1860],{"class":244,"line":251},[242,1861,1862],{},"  try {\n",[242,1864,1865],{"class":244,"line":257},[242,1866,1867],{},"    final Album album = await createAlbum(\"My own album\");\n",[242,1869,1870],{"class":244,"line":264},[242,1871,1872],{},"    print(\"Album ${album.id}'s title is: ${album.title}\");\n",[242,1874,1875],{"class":244,"line":270},[242,1876,1877],{},"  } catch (e) {\n",[242,1879,1880],{"class":244,"line":276},[242,1881,1882],{},"    print(\"There was an exception: $e\");\n",[242,1884,1885],{"class":244,"line":282},[242,1886,881],{},[242,1888,1889],{"class":244,"line":287},[242,1890,296],{},[116,1892,1893,336,1895],{},[220,1894,335],{},[131,1896,1897],{},"examples\u002Fflutter_for_jobseekers_ch07\u002Fbin\u002Fcreate_album.dart",[116,1899,1900],{},"As you can see, sending data to an API is just as easy as retrieving data from the API. These examples are all in pure Dart code, however. Let us see how these examples might be used in the context of a Flutter application.",[150,1902,180],{"id":1903},"flutters-asynchronous-widgets",[116,1905,1906,1907,486,1909,1911,1912,1914],{},"We need some basic boilerplate code to display a Flutter UI that we can work with; otherwise, we are just going to keep both the ",[131,1908,1273],{},[131,1910,1586],{}," methods, and the ",[131,1913,1155],{}," class:",[233,1916,1919],{"className":235,"code":1917,"filename":1918,"language":238,"meta":106,"style":106},"void main() => runApp(MyApp());\n\nclass MyApp extends StatelessWidget {\n  @override\n  Widget build(BuildContext context) {\n    return MaterialApp(\n      title: 'Flutter Demo',\n      debugShowCheckedModeBanner: false,\n      theme: ThemeData(\n        primarySwatch: Colors.blue,\n      ),\n      home: Scaffold(\n        appBar: AppBar(\n          title: const Text('Albums'),\n        ),\n        body: AlbumList(),\n      ),\n    );\n  }\n}\n\nclass AlbumList extends StatelessWidget {\n  @override\n  Widget build(BuildContext context) {\n    return const Text('TODO: Fetch and display a list of albums');\n  }\n}\n","Example 7.15 · Example 7.15.dart",[131,1920,1921,1926,1930,1935,1940,1945,1950,1955,1960,1965,1970,1975,1980,1985,1990,1995,2000,2004,2009,2014,2019,2024,2030,2035,2040,2046,2051],{"__ignoreMap":106},[242,1922,1923],{"class":244,"line":245},[242,1924,1925],{},"void main() => runApp(MyApp());\n",[242,1927,1928],{"class":244,"line":251},[242,1929,261],{"emptyLinePlaceholder":260},[242,1931,1932],{"class":244,"line":257},[242,1933,1934],{},"class MyApp extends StatelessWidget {\n",[242,1936,1937],{"class":244,"line":264},[242,1938,1939],{},"  @override\n",[242,1941,1942],{"class":244,"line":270},[242,1943,1944],{},"  Widget build(BuildContext context) {\n",[242,1946,1947],{"class":244,"line":276},[242,1948,1949],{},"    return MaterialApp(\n",[242,1951,1952],{"class":244,"line":282},[242,1953,1954],{},"      title: 'Flutter Demo',\n",[242,1956,1957],{"class":244,"line":287},[242,1958,1959],{},"      debugShowCheckedModeBanner: false,\n",[242,1961,1962],{"class":244,"line":293},[242,1963,1964],{},"      theme: ThemeData(\n",[242,1966,1967],{"class":244,"line":299},[242,1968,1969],{},"        primarySwatch: Colors.blue,\n",[242,1971,1972],{"class":244,"line":304},[242,1973,1974],{},"      ),\n",[242,1976,1977],{"class":244,"line":310},[242,1978,1979],{},"      home: Scaffold(\n",[242,1981,1982],{"class":244,"line":316},[242,1983,1984],{},"        appBar: AppBar(\n",[242,1986,1987],{"class":244,"line":322},[242,1988,1989],{},"          title: const Text('Albums'),\n",[242,1991,1992],{"class":244,"line":328},[242,1993,1994],{},"        ),\n",[242,1996,1997],{"class":244,"line":902},[242,1998,1999],{},"        body: AlbumList(),\n",[242,2001,2002],{"class":244,"line":1511},[242,2003,1974],{},[242,2005,2006],{"class":244,"line":1516},[242,2007,2008],{},"    );\n",[242,2010,2012],{"class":244,"line":2011},19,[242,2013,881],{},[242,2015,2017],{"class":244,"line":2016},20,[242,2018,296],{},[242,2020,2022],{"class":244,"line":2021},21,[242,2023,261],{"emptyLinePlaceholder":260},[242,2025,2027],{"class":244,"line":2026},22,[242,2028,2029],{},"class AlbumList extends StatelessWidget {\n",[242,2031,2033],{"class":244,"line":2032},23,[242,2034,1939],{},[242,2036,2038],{"class":244,"line":2037},24,[242,2039,1944],{},[242,2041,2043],{"class":244,"line":2042},25,[242,2044,2045],{},"    return const Text('TODO: Fetch and display a list of albums');\n",[242,2047,2049],{"class":244,"line":2048},26,[242,2050,881],{},[242,2052,2054],{"class":244,"line":2053},27,[242,2055,296],{},[124,2057,2058,336,2060,2063,2064,2067,2068,2071,2072,2075,2076,2078,2079,2082],{},[111,2059,611],{},[131,2061,2062],{},"primarySwatch"," no longer colors the app. ",[131,2065,2066],{},"ThemeData.useMaterial3"," \"is ",[220,2069,2070],{},"true"," by default\" (Flutter, n.d.-a), and with Material 3 the theme's ",[131,2073,2074],{},"colorScheme"," defaults to the Material 3 baseline scheme, which ",[131,2077,2062],{}," does not feed into. To get a blue color scheme, use ",[131,2080,2081],{},"ThemeData(colorSchemeSeed: Colors.blue)",", as the runnable copy does.",[225,2084,2086],{"id":2085},"futurebuilder",[131,2087,196],{},[116,2089,2090,2091,486,2093,2096],{},"With our boilerplate code in place, let us first look at how to fetch the albums and display them. For this, we are going to use a pair of widgets: ",[131,2092,196],{},[131,2094,2095],{},"ListView",". We are also going to need to create a method that will let us fetch all the albums in a range of integers:",[233,2098,2101],{"className":235,"code":2099,"filename":2100,"language":238,"meta":106,"style":106},"Future\u003CList\u003CAlbum>> fetchAlbums() async {\n  final List\u003Cint> albumsToFetch = List\u003Cint>.generate(20, (i) => i + 1);\n  final List\u003CAlbum> albums = [];\n\n  for (final int albumToFetch in albumsToFetch) {\n    final Album? album = await fetchAlbum(id: albumToFetch);\n    if (album != null) albums.add(album);\n  }\n  return albums;\n}\n","Example 7.16 · Example 7.16.dart",[131,2102,2103,2108,2113,2118,2122,2126,2130,2135,2139,2144],{"__ignoreMap":106},[242,2104,2105],{"class":244,"line":245},[242,2106,2107],{},"Future\u003CList\u003CAlbum>> fetchAlbums() async {\n",[242,2109,2110],{"class":244,"line":251},[242,2111,2112],{},"  final List\u003Cint> albumsToFetch = List\u003Cint>.generate(20, (i) => i + 1);\n",[242,2114,2115],{"class":244,"line":257},[242,2116,2117],{},"  final List\u003CAlbum> albums = [];\n",[242,2119,2120],{"class":244,"line":264},[242,2121,261],{"emptyLinePlaceholder":260},[242,2123,2124],{"class":244,"line":270},[242,2125,1552],{},[242,2127,2128],{"class":244,"line":276},[242,2129,1557],{},[242,2131,2132],{"class":244,"line":282},[242,2133,2134],{},"    if (album != null) albums.add(album);\n",[242,2136,2137],{"class":244,"line":287},[242,2138,881],{},[242,2140,2141],{"class":244,"line":293},[242,2142,2143],{},"  return albums;\n",[242,2145,2146],{"class":244,"line":299},[242,2147,296],{},[116,2149,2150,2151,2154,2155,2158,2159,2161,2162,2165],{},"You may notice some code in there that you do not yet recognize: the ",[131,2152,2153],{},"List.generate"," method. By giving it an integer (in this example, 20), it runs a method that many times and performs some action. The second parameter, (",[131,2156,2157],{},"i","), assigns the value of the current list index to the variable ",[131,2160,2157],{},", then returns a value of ",[131,2163,2164],{},"i+1"," and adds it to the list. This is repeated until the process has run for the number of iterations specified. The result is a list of integers ranging from 1 to 20. This is the list of albums that we will be fetching for our eventual list.",[658,2167,2168,2171,2172,2175],{},[111,2169,2170],{},"Extra credit:"," how could you expand this to get the next ",[220,2173,2174],{},"page"," of results, albums 21-40?",[116,2177,2178,2179,2181,2182,2184],{},"Now that we have got a method that will fetch a given number of albums, we need a widget that will run that method for us. This is where ",[131,2180,196],{}," comes into play. ",[131,2183,196],{}," has two parameters:",[753,2186,2187,2195],{},[756,2188,2190,2191,2194],{"name":2189,"type":214},"future","Takes in the object that returns a future, which in our case will be the ",[131,2192,2193],{},"fetchAlbums"," method.",[756,2196,2199,2200,1407,2202,2205,2206,699,2209,2211],{"name":2197,"type":2198},"builder","method","Will return a widget and has access to the ",[131,2201,196],{},[131,2203,2204],{},"AsyncSnapshot"," data as well as the current ",[131,2207,2208],{},"BuildContext",[131,2210,2204],{}," has some very useful information that we will use to decide what widget to display to the user.",[116,2213,2214],{},"Let us look at all this in action:",[233,2216,2219],{"className":235,"code":2217,"filename":2218,"language":238,"meta":106,"style":106},"class AlbumList extends StatelessWidget {\n  @override\n  Widget build(BuildContext context) {\n    return FutureBuilder(\n      future: fetchAlbums(),\n      builder: (BuildContext context, AsyncSnapshot snapshot) {\n        if (snapshot.connectionState == ConnectionState.waiting) {\n          return const Center(\n            child: CircularProgressIndicator(),\n          );\n        }\n        if (snapshot.connectionState == ConnectionState.done &&\n          snapshot.hasData) {\n          return const Text(\"Done loading data\");\n        }\n        return const Text(\"There was a problem loading the album list.\");\n      },\n    );\n  }\n}\n","Example 7.17 · Example 7.17.dart",[131,2220,2221,2225,2229,2233,2238,2243,2248,2253,2258,2263,2268,2273,2278,2283,2288,2292,2297,2302,2306,2310],{"__ignoreMap":106},[242,2222,2223],{"class":244,"line":245},[242,2224,2029],{},[242,2226,2227],{"class":244,"line":251},[242,2228,1939],{},[242,2230,2231],{"class":244,"line":257},[242,2232,1944],{},[242,2234,2235],{"class":244,"line":264},[242,2236,2237],{},"    return FutureBuilder(\n",[242,2239,2240],{"class":244,"line":270},[242,2241,2242],{},"      future: fetchAlbums(),\n",[242,2244,2245],{"class":244,"line":276},[242,2246,2247],{},"      builder: (BuildContext context, AsyncSnapshot snapshot) {\n",[242,2249,2250],{"class":244,"line":282},[242,2251,2252],{},"        if (snapshot.connectionState == ConnectionState.waiting) {\n",[242,2254,2255],{"class":244,"line":287},[242,2256,2257],{},"          return const Center(\n",[242,2259,2260],{"class":244,"line":293},[242,2261,2262],{},"            child: CircularProgressIndicator(),\n",[242,2264,2265],{"class":244,"line":299},[242,2266,2267],{},"          );\n",[242,2269,2270],{"class":244,"line":304},[242,2271,2272],{},"        }\n",[242,2274,2275],{"class":244,"line":310},[242,2276,2277],{},"        if (snapshot.connectionState == ConnectionState.done &&\n",[242,2279,2280],{"class":244,"line":316},[242,2281,2282],{},"          snapshot.hasData) {\n",[242,2284,2285],{"class":244,"line":322},[242,2286,2287],{},"          return const Text(\"Done loading data\");\n",[242,2289,2290],{"class":244,"line":328},[242,2291,2272],{},[242,2293,2294],{"class":244,"line":902},[242,2295,2296],{},"        return const Text(\"There was a problem loading the album list.\");\n",[242,2298,2299],{"class":244,"line":1511},[242,2300,2301],{},"      },\n",[242,2303,2304],{"class":244,"line":1516},[242,2305,2008],{},[242,2307,2308],{"class":244,"line":2011},[242,2309,881],{},[242,2311,2312],{"class":244,"line":2016},[242,2313,296],{},[116,2315,2316,2317,2319],{},"We are mostly interested in the ",[131,2318,2197],{}," method right now, as the rest is self-explanatory.",[1701,2321,2322,2326,2336,2340,2346,2350],{"level":1703},[1705,2323,2325],{"id":2324},"waiting","Waiting",[116,2327,2328,2329,2332,2333,2335],{},"The first check will return a ",[131,2330,2331],{},"CircularProgressIndicator"," while we are waiting for the ",[131,2334,214],{}," to complete.",[1705,2337,2339],{"id":2338},"done","Done",[116,2341,2342,2343,2345],{},"The second check will return the widget we want to display once the ",[131,2344,214],{}," has successfully completed.",[1705,2347,2349],{"id":2348},"anything-else","Anything else",[116,2351,2352],{},"If neither of those states can be satisfied (such as in the case where the connection times out, there is no data returned, and so on), an error message will be shown to the user.",[380,2354,2355,2356,2358,2359,2365],{},"Remember, the ",[131,2357,2197],{}," must ",[111,2360,2361,2364],{},[220,2362,2363],{},"always"," return a widget",", which is why we have a guard at the bottom that returns an error message.",[124,2367,2368,336,2370,2373,2374,2377,2378,2381,2382,2385,2386,2389,2390,503],{},[111,2369,611],{},[131,2371,2372],{},"fetchAlbums()"," should not be called inside ",[131,2375,2376],{},"build",". \"The future must have been obtained earlier, e.g. during State.initState, State.didUpdateWidget, or State.didChangeDependencies. ",[242,2379,2380],{},"..."," If the future is created at the same time as the FutureBuilder, then every time the FutureBuilder's parent is rebuilt, the asynchronous task will be restarted\" (Flutter, n.d.-b). As printed, every rebuild fetches all 20 albums again. The runnable copy makes ",[131,2383,2384],{},"AlbumList"," a ",[131,2387,2388],{},"StatefulWidget"," that starts the future once in ",[131,2391,2392],{},"initState",[225,2394,2396],{"id":2395},"listview",[131,2397,2095],{},[116,2399,2400,2401,2403],{},"At this point, we have a widget which is calling the API, retrieves the list of albums, and handles the waiting and error states. Next, we need to replace the placeholder text with a list of the album titles that were received. We are going to do this with the ",[131,2402,2095],{}," widget.",[233,2405,2408],{"className":235,"code":2406,"filename":2407,"language":238,"meta":106,"style":106},"if (snapshot.connectionState == ConnectionState.done && snapshot.hasData) {\n  final List\u003CAlbum> albums = snapshot.data;\n  return ListView.builder(\n    itemCount: albums.length,\n    itemBuilder: (BuildContext context, int index) => ListTile(\n      title: Text(albums[index].title ?? \"Undefined\"),\n    ),\n  );\n}\n","Example 7.18 · Example 7.18.dart",[131,2409,2410,2415,2420,2425,2430,2435,2440,2445,2449],{"__ignoreMap":106},[242,2411,2412],{"class":244,"line":245},[242,2413,2414],{},"if (snapshot.connectionState == ConnectionState.done && snapshot.hasData) {\n",[242,2416,2417],{"class":244,"line":251},[242,2418,2419],{},"  final List\u003CAlbum> albums = snapshot.data;\n",[242,2421,2422],{"class":244,"line":257},[242,2423,2424],{},"  return ListView.builder(\n",[242,2426,2427],{"class":244,"line":264},[242,2428,2429],{},"    itemCount: albums.length,\n",[242,2431,2432],{"class":244,"line":270},[242,2433,2434],{},"    itemBuilder: (BuildContext context, int index) => ListTile(\n",[242,2436,2437],{"class":244,"line":276},[242,2438,2439],{},"      title: Text(albums[index].title ?? \"Undefined\"),\n",[242,2441,2442],{"class":244,"line":282},[242,2443,2444],{},"    ),\n",[242,2446,2447],{"class":244,"line":287},[242,2448,1223],{},[242,2450,2451],{"class":244,"line":293},[242,2452,296],{},[658,2454,2455,2458,2459,2462,2463,2466],{},[131,2456,2457],{},"ListView.builder"," is a magical little widget. It builds a scrollable list and ",[111,2460,2461],{},"only builds the exact number of widgets that can be displayed on the screen",". For this reason, it is highly performant with very long lists, especially in contrast to something like a ",[131,2464,2465],{},"Column",", which builds all its children all the time, even when they are not on screen.",[116,2468,2469,2470,2473,2474,2476,2477,2480,2481,2484,2485,2487,2488,2491],{},"By specifying an ",[131,2471,2472],{},"itemCount",", the ",[131,2475,2457],{}," knows precisely how many items it will have to build. It uses that information to iterate through the ",[131,2478,2479],{},"itemBuilder"," method, passing the current ",[131,2482,2483],{},"index"," of the item it is building as a parameter. We can use that ",[131,2486,2483],{}," to grab the corresponding item from our list, which we do with ",[131,2489,2490],{},"albums[index]"," in this example. The result is that a scrollable, performant list of the album titles is generated automatically for us.",[116,2493,2494,2495,2498,2499,2501,2502,2505],{},"We chose to use a ",[131,2496,2497],{},"ListTile"," to display the title of the album, but any widget will do. Try swapping the ",[131,2500,2497],{}," for a ",[131,2503,2504],{},"Card"," widget for some added dimension to the resulting list. The title parameter will need to be swapped for a child parameter, and the content will benefit from some extra padding, like the following:",[233,2507,2510],{"className":235,"code":2508,"filename":2509,"language":238,"meta":106,"style":106},"if (snapshot.connectionState == ConnectionState.done && snapshot.hasData) {\n  final List\u003CAlbum> albums = snapshot.data;\n  return ListView.builder(\n    itemCount: albums.length,\n    itemBuilder: (BuildContext context, int index) => Card(\n      child: Padding(\n        padding: const EdgeInsets.all(8),\n        child: Text(albums[index].title ?? \"Undefined\"),\n      ),\n    ),\n  );\n}\n","Example 7.19 · Example 7.19.dart",[131,2511,2512,2516,2520,2524,2528,2533,2538,2543,2548,2552,2556,2560],{"__ignoreMap":106},[242,2513,2514],{"class":244,"line":245},[242,2515,2414],{},[242,2517,2518],{"class":244,"line":251},[242,2519,2419],{},[242,2521,2522],{"class":244,"line":257},[242,2523,2424],{},[242,2525,2526],{"class":244,"line":264},[242,2527,2429],{},[242,2529,2530],{"class":244,"line":270},[242,2531,2532],{},"    itemBuilder: (BuildContext context, int index) => Card(\n",[242,2534,2535],{"class":244,"line":276},[242,2536,2537],{},"      child: Padding(\n",[242,2539,2540],{"class":244,"line":282},[242,2541,2542],{},"        padding: const EdgeInsets.all(8),\n",[242,2544,2545],{"class":244,"line":287},[242,2546,2547],{},"        child: Text(albums[index].title ?? \"Undefined\"),\n",[242,2549,2550],{"class":244,"line":293},[242,2551,1974],{},[242,2553,2554],{"class":244,"line":299},[242,2555,2444],{},[242,2557,2558],{"class":244,"line":304},[242,2559,1223],{},[242,2561,2562],{"class":244,"line":310},[242,2563,296],{},[116,2565,2566,336,2568,2571,2572,2574,2575,344],{},[220,2567,335],{},[131,2569,2570],{},"examples\u002Fflutter_for_jobseekers_ch07\u002Flib\u002Ffuture_builder\u002Fmain_future.dart"," (the whole ",[131,2573,196],{}," version; run it with ",[131,2576,2577],{},"flutter run -t lib\u002Ffuture_builder\u002Fmain_future.dart",[225,2579,2581,2582],{"id":2580},"adding-a-floatingactionbutton","Adding a ",[131,2583,2584],{},"FloatingActionButton",[116,2586,2587,2588,2590,2591,2594,2595,2597,2598,2601,2602,2604,2605,1082],{},"Now that we can display a list of albums, we should add a way for us to create an album. Let us add a ",[131,2589,2584],{}," to our ",[131,2592,2593],{},"Scaffold",", so we can add an album from anywhere. However, to properly pass the ",[131,2596,2208],{}," into the ",[131,2599,2600],{},"onPressed"," method, we will want to break out the ",[131,2603,2593],{}," into its own widget, which we will call ",[131,2606,2607],{},"Home",[233,2609,2612],{"className":235,"code":2610,"filename":2611,"language":238,"meta":106,"style":106},"class MyApp extends StatelessWidget {\n  const MyApp({super.key});\n\n  @override\n  Widget build(BuildContext context) {\n    return MaterialApp(\n      title: 'Flutter Demo',\n      debugShowCheckedModeBanner: false,\n      theme: ThemeData(\n        primarySwatch: Colors.blue,\n      ),\n      home: const Home(),\n    );\n  }\n}\n\nclass Home extends StatelessWidget {\n  const Home({super.key});\n\n  @override\n  Widget build(BuildContext context) {\n    return Scaffold(\n      appBar: AppBar(\n        title: const Text('Albums'),\n      ),\n      floatingActionButton: FloatingActionButton(\n        onPressed: () async {\n          \u002F\u002F TODO: Implement dialog to get an album title, then create the album\n        },\n        child: const Icon(Icons.add),\n      ),\n      body: const AlbumList(),\n    );\n  }\n}\n","Example 7.20 · Example 7.20.dart",[131,2613,2614,2618,2623,2627,2631,2635,2639,2643,2647,2651,2655,2659,2664,2668,2672,2676,2680,2685,2690,2694,2698,2702,2707,2712,2717,2721,2726,2731,2737,2743,2749,2754,2760,2765,2770],{"__ignoreMap":106},[242,2615,2616],{"class":244,"line":245},[242,2617,1934],{},[242,2619,2620],{"class":244,"line":251},[242,2621,2622],{},"  const MyApp({super.key});\n",[242,2624,2625],{"class":244,"line":257},[242,2626,261],{"emptyLinePlaceholder":260},[242,2628,2629],{"class":244,"line":264},[242,2630,1939],{},[242,2632,2633],{"class":244,"line":270},[242,2634,1944],{},[242,2636,2637],{"class":244,"line":276},[242,2638,1949],{},[242,2640,2641],{"class":244,"line":282},[242,2642,1954],{},[242,2644,2645],{"class":244,"line":287},[242,2646,1959],{},[242,2648,2649],{"class":244,"line":293},[242,2650,1964],{},[242,2652,2653],{"class":244,"line":299},[242,2654,1969],{},[242,2656,2657],{"class":244,"line":304},[242,2658,1974],{},[242,2660,2661],{"class":244,"line":310},[242,2662,2663],{},"      home: const Home(),\n",[242,2665,2666],{"class":244,"line":316},[242,2667,2008],{},[242,2669,2670],{"class":244,"line":322},[242,2671,881],{},[242,2673,2674],{"class":244,"line":328},[242,2675,296],{},[242,2677,2678],{"class":244,"line":902},[242,2679,261],{"emptyLinePlaceholder":260},[242,2681,2682],{"class":244,"line":1511},[242,2683,2684],{},"class Home extends StatelessWidget {\n",[242,2686,2687],{"class":244,"line":1516},[242,2688,2689],{},"  const Home({super.key});\n",[242,2691,2692],{"class":244,"line":2011},[242,2693,261],{"emptyLinePlaceholder":260},[242,2695,2696],{"class":244,"line":2016},[242,2697,1939],{},[242,2699,2700],{"class":244,"line":2021},[242,2701,1944],{},[242,2703,2704],{"class":244,"line":2026},[242,2705,2706],{},"    return Scaffold(\n",[242,2708,2709],{"class":244,"line":2032},[242,2710,2711],{},"      appBar: AppBar(\n",[242,2713,2714],{"class":244,"line":2037},[242,2715,2716],{},"        title: const Text('Albums'),\n",[242,2718,2719],{"class":244,"line":2042},[242,2720,1974],{},[242,2722,2723],{"class":244,"line":2048},[242,2724,2725],{},"      floatingActionButton: FloatingActionButton(\n",[242,2727,2728],{"class":244,"line":2053},[242,2729,2730],{},"        onPressed: () async {\n",[242,2732,2734],{"class":244,"line":2733},28,[242,2735,2736],{},"          \u002F\u002F TODO: Implement dialog to get an album title, then create the album\n",[242,2738,2740],{"class":244,"line":2739},29,[242,2741,2742],{},"        },\n",[242,2744,2746],{"class":244,"line":2745},30,[242,2747,2748],{},"        child: const Icon(Icons.add),\n",[242,2750,2752],{"class":244,"line":2751},31,[242,2753,1974],{},[242,2755,2757],{"class":244,"line":2756},32,[242,2758,2759],{},"      body: const AlbumList(),\n",[242,2761,2763],{"class":244,"line":2762},33,[242,2764,2008],{},[242,2766,2768],{"class":244,"line":2767},34,[242,2769,881],{},[242,2771,2773],{"class":244,"line":2772},35,[242,2774,296],{},[225,2776,2778],{"id":2777},"showing-a-dialog","Showing a dialog",[116,2780,2781,2782,2785,2786,2789,2790,2793,2794,2796,2797,2799,2800,2802,2803,2805],{},"Flutter offers a ",[131,2783,2784],{},"showDialog"," method for us to display a popup where we can collect information from the user. This is an ",[111,2787,2788],{},"asynchronous method"," that will display a ",[131,2791,2792],{},"Dialog"," over the UI and can return some sort of value, if desired. It takes in a ",[131,2795,2208],{}," and uses that context in its ",[131,2798,2197],{}," method. The ",[131,2801,2197],{}," method returns the ",[131,2804,2792],{}," itself. We can leverage this system to create a popup that asks the user to enter the name of an album.",[116,2807,2808,2809,2811],{},"Let us start by building the helper method that will show the ",[131,2810,2792],{}," and return the value:",[233,2813,2816],{"className":235,"code":2814,"filename":2815,"language":238,"meta":106,"style":106},"Future\u003CString?> _userInputDialog(BuildContext context) async {\n  return await showDialog\u003CString>(\n    context: context,\n    builder: (BuildContext context) {\n      return SimpleDialog(\n        title: const Text('Create Album'),\n        contentPadding: const EdgeInsets.all(24),\n        children: [\n          TextFormField(\n            decoration: const InputDecoration(\n              hintText: \"Album name\",\n            ),\n            onFieldSubmitted: (String? value) {\n              Navigator.pop(context, value);\n            },\n          ),\n        ],\n      );\n    },\n  );\n}\n","Example 7.21 · Example 7.21.dart",[131,2817,2818,2823,2828,2833,2838,2843,2848,2853,2858,2863,2868,2873,2878,2883,2888,2893,2898,2903,2908,2912,2916],{"__ignoreMap":106},[242,2819,2820],{"class":244,"line":245},[242,2821,2822],{},"Future\u003CString?> _userInputDialog(BuildContext context) async {\n",[242,2824,2825],{"class":244,"line":251},[242,2826,2827],{},"  return await showDialog\u003CString>(\n",[242,2829,2830],{"class":244,"line":257},[242,2831,2832],{},"    context: context,\n",[242,2834,2835],{"class":244,"line":264},[242,2836,2837],{},"    builder: (BuildContext context) {\n",[242,2839,2840],{"class":244,"line":270},[242,2841,2842],{},"      return SimpleDialog(\n",[242,2844,2845],{"class":244,"line":276},[242,2846,2847],{},"        title: const Text('Create Album'),\n",[242,2849,2850],{"class":244,"line":282},[242,2851,2852],{},"        contentPadding: const EdgeInsets.all(24),\n",[242,2854,2855],{"class":244,"line":287},[242,2856,2857],{},"        children: [\n",[242,2859,2860],{"class":244,"line":293},[242,2861,2862],{},"          TextFormField(\n",[242,2864,2865],{"class":244,"line":299},[242,2866,2867],{},"            decoration: const InputDecoration(\n",[242,2869,2870],{"class":244,"line":304},[242,2871,2872],{},"              hintText: \"Album name\",\n",[242,2874,2875],{"class":244,"line":310},[242,2876,2877],{},"            ),\n",[242,2879,2880],{"class":244,"line":316},[242,2881,2882],{},"            onFieldSubmitted: (String? value) {\n",[242,2884,2885],{"class":244,"line":322},[242,2886,2887],{},"              Navigator.pop(context, value);\n",[242,2889,2890],{"class":244,"line":328},[242,2891,2892],{},"            },\n",[242,2894,2895],{"class":244,"line":902},[242,2896,2897],{},"          ),\n",[242,2899,2900],{"class":244,"line":1511},[242,2901,2902],{},"        ],\n",[242,2904,2905],{"class":244,"line":1516},[242,2906,2907],{},"      );\n",[242,2909,2910],{"class":244,"line":2011},[242,2911,1623],{},[242,2913,2914],{"class":244,"line":2016},[242,2915,1223],{},[242,2917,2918],{"class":244,"line":2021},[242,2919,296],{},[116,2921,2922,2923,2925,2926,2929,2930,2932,2933,2935,2936,2939],{},"This simple ",[131,2924,2792],{}," will have a title and a text input. We are using a ",[131,2927,2928],{},"SimpleDialog",", which is a subclass of the ",[131,2931,2792],{},". It provides us with the necessary options we need, but there are other types of ",[131,2934,2792],{},"s you could use, such as the ",[131,2937,2938],{},"AlertDialog"," for example.",[155,2941,2942,2951],{},[158,2943,2946,2947,2950],{"icon":2944,"title":2945},"i-lucide-corner-down-left","Submit","For the user to submit their input, they simply need to press the ",[220,2948,2949],{},"Enter"," key on the keyboard once they are finished.",[158,2952,2955],{"icon":2953,"title":2954},"i-lucide-x","Cancel","To cancel or dismiss the dialog, they can tap or click anywhere outside of the dialog.",[116,2957,2958,2959,2961,2962,1082],{},"With our helper function in place, we only need to hook it up to the ",[131,2960,2600],{}," method of our ",[131,2963,2584],{},[233,2965,2968],{"className":235,"code":2966,"filename":2967,"language":238,"meta":106,"style":106},"class Home extends StatelessWidget {\n  const Home({super.key});\n\n  @override\n  Widget build(BuildContext context) {\n    return Scaffold(\n      appBar: AppBar(\n        title: const Text('Albums'),\n      ),\n      floatingActionButton: FloatingActionButton(\n        onPressed: () async {\n          final String? title = await _userInputDialog(context);\n          if (title == null) {\n            throw Exception('Cancelled Album creation.');\n          }\n\n          if (title.isNotEmpty) {\n            final Album album = await createAlbum(title);\n            print(\"Created new album! ${album.title}\");\n          }\n        },\n        child: const Icon(Icons.add),\n      ),\n      body: const AlbumList(),\n    );\n  }\n}\n","Example 7.22 · Example 7.22.dart",[131,2969,2970,2974,2978,2982,2986,2990,2994,2998,3002,3006,3010,3014,3019,3024,3029,3034,3038,3043,3048,3053,3057,3061,3065,3069,3073,3077,3081],{"__ignoreMap":106},[242,2971,2972],{"class":244,"line":245},[242,2973,2684],{},[242,2975,2976],{"class":244,"line":251},[242,2977,2689],{},[242,2979,2980],{"class":244,"line":257},[242,2981,261],{"emptyLinePlaceholder":260},[242,2983,2984],{"class":244,"line":264},[242,2985,1939],{},[242,2987,2988],{"class":244,"line":270},[242,2989,1944],{},[242,2991,2992],{"class":244,"line":276},[242,2993,2706],{},[242,2995,2996],{"class":244,"line":282},[242,2997,2711],{},[242,2999,3000],{"class":244,"line":287},[242,3001,2716],{},[242,3003,3004],{"class":244,"line":293},[242,3005,1974],{},[242,3007,3008],{"class":244,"line":299},[242,3009,2725],{},[242,3011,3012],{"class":244,"line":304},[242,3013,2730],{},[242,3015,3016],{"class":244,"line":310},[242,3017,3018],{},"          final String? title = await _userInputDialog(context);\n",[242,3020,3021],{"class":244,"line":316},[242,3022,3023],{},"          if (title == null) {\n",[242,3025,3026],{"class":244,"line":322},[242,3027,3028],{},"            throw Exception('Cancelled Album creation.');\n",[242,3030,3031],{"class":244,"line":328},[242,3032,3033],{},"          }\n",[242,3035,3036],{"class":244,"line":902},[242,3037,261],{"emptyLinePlaceholder":260},[242,3039,3040],{"class":244,"line":1511},[242,3041,3042],{},"          if (title.isNotEmpty) {\n",[242,3044,3045],{"class":244,"line":1516},[242,3046,3047],{},"            final Album album = await createAlbum(title);\n",[242,3049,3050],{"class":244,"line":2011},[242,3051,3052],{},"            print(\"Created new album! ${album.title}\");\n",[242,3054,3055],{"class":244,"line":2016},[242,3056,3033],{},[242,3058,3059],{"class":244,"line":2021},[242,3060,2742],{},[242,3062,3063],{"class":244,"line":2026},[242,3064,2748],{},[242,3066,3067],{"class":244,"line":2032},[242,3068,1974],{},[242,3070,3071],{"class":244,"line":2037},[242,3072,2759],{},[242,3074,3075],{"class":244,"line":2042},[242,3076,2008],{},[242,3078,3079],{"class":244,"line":2048},[242,3080,881],{},[242,3082,3083],{"class":244,"line":2053},[242,3084,296],{},[116,3086,3087,3088,3090,3091,3093,3094,3096,3097,3099,3100,3102,3103,3105,3106,3108,3109,3111],{},"Since we made our ",[131,3089,2600],{}," method an ",[131,3092,485],{},", we can ",[131,3095,489],{}," the return value of the ",[131,3098,2792],{}," as a nullable ",[131,3101,1144],{}," (remember, the user can always cancel.) After a quick check to make sure the title is not empty, we can ",[131,3104,489],{}," our ",[131,3107,1586],{}," method with the new title, returning a newly created ",[131,3110,1155],{},". Finally, we print the album details out to prove that it was, indeed, created.",[380,3113,3114,3115,3118],{},"The only problem is that the new album is ",[111,3116,3117],{},"not added to the list",". For that, we are going to need to talk about streams.",[150,3120,183],{"id":3121},"sinks-and-streams",[116,3123,3124,3125,593,3127,3129,3130,593,3133,3136],{},"Sibling to the ",[131,3126,485],{},[131,3128,489],{}," operators is the ",[131,3131,3132],{},"async*",[131,3134,3135],{},"yield"," operators. Let us look at an analogy to understand the relationship between these concepts.",[155,3138,3139,3150],{},[158,3140,3143,3144,3146,3147,3149],{"icon":3141,"title":3142},"i-lucide-glass-water","async \u002F await — filling a glass","Let us suppose that you are thirsty and would like a glass of water. You pick up the glass, fill it with water, and once it is full you take a drink. You can do other things while you are waiting for the glass to fill, but you cannot take a drink until it is full. This is ",[131,3145,485],{}," (you can do other things while you wait for the glass to fill) and ",[131,3148,489],{}," (you must wait for the glass to fill completely before taking a drink).",[158,3151,3154,3155,3157,3158,3160],{"icon":3152,"title":3153},"i-lucide-citrus","async* \u002F yield — drinking through a straw","Now, suppose you want to drink from the glass while it is being filled. Not only that, but you have got a button you can push that will add lemonade into the stream of water. Each time you press the button, a bit of lemonade is added to the stream. Refreshing! To take a drink while your glass is being filled, you grab a straw and start sucking on it while the water fills the glass. Then, you decide to push the button to add some lemonade to the mix. This is ",[131,3156,3132],{}," (the act of filling the glass does not block you from consuming the beverage) and ",[131,3159,3135],{}," (pushing the button adds more things into the stream in a process called a sink, which is then emitted, or yielded, from the stream).",[116,3162,3163,3164,3166,3167,3169,3170,3172,3173,3175,3176,3178],{},"In our previous example, our ",[131,3165,2457],{}," used the data from the ",[131,3168,196],{}," to compile a list of ",[131,3171,1155],{},"s, which it then used to build the individual ",[131,3174,2504],{},"s displaying the album titles. We displayed a ",[131,3177,2331],{}," while the albums were being fetched from the API, which prevented us from showing any album until they were all loaded.",[380,3180,3181,3182,3184,3185,3188],{},"Furthermore, once the ",[131,3183,2193],{}," method has completed, the list of albums is ",[111,3186,3187],{},"finalized",", meaning we cannot add anything new to it without fetching everything all over again. So much for adding our newly created album to the list.",[225,3190,352,3192],{"id":3191},"the-streamcontroller",[131,3193,200],{},[116,3195,3196,3197,3200,3201,3203,3204,1082],{},"Clearly, we are going to need a different approach to accomplish what we want here. Let us refactor our code to use sinks and streams. The first thing we will need to do is pull in the ",[131,3198,3199],{},"dart:async"," library to get access to the ",[131,3202,200],{}," widget. This widget will allow us to control our stream. After we have imported the library, we can go ahead and create a new ",[131,3205,200],{},[233,3207,3210],{"className":235,"code":3208,"filename":3209,"language":238,"meta":106,"style":106},"import 'dart:convert';\nimport 'dart:async';\nimport 'package:flutter\u002Fmaterial.dart';\nimport 'package:http\u002Fhttp.dart';\n\nvoid main() => runApp(const MyApp());\nClient http = Client();\nfinal StreamController streamController = StreamController();\n...\n","Example 7.23 · Example 7.23.dart",[131,3211,3212,3216,3221,3226,3230,3234,3239,3243,3248],{"__ignoreMap":106},[242,3213,3214],{"class":244,"line":245},[242,3215,248],{},[242,3217,3218],{"class":244,"line":251},[242,3219,3220],{},"import 'dart:async';\n",[242,3222,3223],{"class":244,"line":257},[242,3224,3225],{},"import 'package:flutter\u002Fmaterial.dart';\n",[242,3227,3228],{"class":244,"line":264},[242,3229,839],{},[242,3231,3232],{"class":244,"line":270},[242,3233,261],{"emptyLinePlaceholder":260},[242,3235,3236],{"class":244,"line":276},[242,3237,3238],{},"void main() => runApp(const MyApp());\n",[242,3240,3241],{"class":244,"line":282},[242,3242,848],{},[242,3244,3245],{"class":244,"line":287},[242,3246,3247],{},"final StreamController streamController = StreamController();\n",[242,3249,3250],{"class":244,"line":293},[242,3251,3252],{},"...\n",[124,3254,3255,336,3257,3259,3260,3262,3263,503],{},[111,3256,611],{},[131,3258,200],{}," is not a widget. It is a plain Dart class from ",[131,3261,3199],{},", \"A controller with the stream it controls\" (Dart, n.d.-d); the widget in this section is ",[131,3264,3265],{},"StreamBuilder",[116,3267,3268,3269,3271,3272,3274,3275,1082],{},"We will use this ",[131,3270,200],{}," to add our events to the stream's sink and to listen to whatever streams back out. Let us start by reworking the ",[131,3273,2193],{}," method to use the new ",[131,3276,200],{},[163,3278,3279,3285,3294,3309],{},[166,3280,3281,3282,503],{},"Since we are going to be sending a sink event instead of returning a value, our return type becomes ",[131,3283,3284],{},"void",[166,3286,3287,3288,3290,3291,3293],{},"We still need the ",[131,3289,485],{}," keyword because we will be ",[131,3292,489],{},"ing each album as we fetch it from the API, though.",[166,3295,3296,3297,3299,3300,3302,3303,3306,3307,1007],{},"The other major change is that we no longer keep track of all the ",[131,3298,1155],{},"s that come back from the API. Instead, we pass each of them off to the ",[131,3301,200],{}," to deal with. This means we no longer need our ",[131,3304,3305],{},"List\u003CAlbum>"," variable, and we can remove the ",[131,3308,528],{},[166,3310,3311,3312,3314],{},"In place of those, we will take the album we get back and add an event to the ",[131,3313,200],{},"'s sink:",[233,3316,3318],{"className":235,"code":3317,"filename":3209,"language":238,"meta":106,"style":106},"void fetchAlbums() async {\n  final List\u003Cint> albumsToFetch = List\u003Cint>.generate(10, (k) => k + 1);\n\n  for (final int albumToFetch in albumsToFetch) {\n    final Album? album = await fetchAlbum(id: albumToFetch);\n    streamController.sink.add(album);\n  }\n}\n",[131,3319,3320,3325,3330,3334,3338,3342,3347,3351],{"__ignoreMap":106},[242,3321,3322],{"class":244,"line":245},[242,3323,3324],{},"void fetchAlbums() async {\n",[242,3326,3327],{"class":244,"line":251},[242,3328,3329],{},"  final List\u003Cint> albumsToFetch = List\u003Cint>.generate(10, (k) => k + 1);\n",[242,3331,3332],{"class":244,"line":257},[242,3333,261],{"emptyLinePlaceholder":260},[242,3335,3336],{"class":244,"line":264},[242,3337,1552],{},[242,3339,3340],{"class":244,"line":270},[242,3341,1557],{},[242,3343,3344],{"class":244,"line":276},[242,3345,3346],{},"    streamController.sink.add(album);\n",[242,3348,3349],{"class":244,"line":282},[242,3350,881],{},[242,3352,3353],{"class":244,"line":287},[242,3354,296],{},[225,3356,352,3358,3360],{"id":3357},"the-async-alternative",[131,3359,3132],{}," alternative",[116,3362,3363,3364,3366,3367,3369,3370,3373,3374,3376,3377,3379,3380,3382],{},"Note that we could technically add the event directly to the ",[131,3365,200],{},", too. This way, however, we are being more verbose for the sake of education. If we were not concerned about adding new events to our sink later, we could forego the ",[131,3368,200],{}," entirely. Instead, we would opt for a return value of ",[131,3371,3372],{},"Stream\u003CAlbum>"," and use the ",[131,3375,3132],{}," keyword. Then, instead of adding an event to the ",[131,3378,200],{},"'s sink, we would ",[131,3381,3135],{}," the album. That code would look like the following:",[233,3384,3387],{"className":235,"code":3385,"filename":3386,"language":238,"meta":106,"style":106},"Stream\u003CAlbum> fetchAlbums() async* {\n  final List\u003Cint> albumsToFetch = List\u003Cint>.generate(10, (k) => k + 1);\n\n  for (final int albumToFetch in albumsToFetch) {\n    final Album? album = await fetchAlbum(id: albumToFetch);\n    yield album;\n  }\n}\n","Example 7.25 · Example 7.25.dart",[131,3388,3389,3394,3398,3402,3406,3410,3415,3419],{"__ignoreMap":106},[242,3390,3391],{"class":244,"line":245},[242,3392,3393],{},"Stream\u003CAlbum> fetchAlbums() async* {\n",[242,3395,3396],{"class":244,"line":251},[242,3397,3329],{},[242,3399,3400],{"class":244,"line":257},[242,3401,261],{"emptyLinePlaceholder":260},[242,3403,3404],{"class":244,"line":264},[242,3405,1552],{},[242,3407,3408],{"class":244,"line":270},[242,3409,1557],{},[242,3411,3412],{"class":244,"line":276},[242,3413,3414],{},"    yield album;\n",[242,3416,3417],{"class":244,"line":282},[242,3418,881],{},[242,3420,3421],{"class":244,"line":287},[242,3422,296],{},[116,3424,3425,3427,3428,3431,3432,3434],{},[220,3426,128],{}," Example 7.25 declares ",[131,3429,3430],{},"Stream\u003CAlbum?>"," instead, so it also yields the albums that were not found (",[131,3433,363],{},").",[116,3436,3437,336,3439],{},[220,3438,335],{},[131,3440,3441],{},"examples\u002Fflutter_for_jobseekers_ch07\u002Flib\u002Falbum_generator.dart",[124,3443,3444,3446,3447,3450,3451,3454,3455,3457,3458,3460,3461,2082],{},[111,3445,611],{}," this does not compile. ",[131,3448,3449],{},"album"," is an ",[131,3452,3453],{},"Album?",", and a ",[131,3456,3372],{}," can only yield an ",[131,3459,1155],{},". Yield only the albums that were found: ",[131,3462,3463],{},"if (album != null) yield album;",[116,3465,3466,3467,3469,3470,3472,3473,3475],{},"At that point, we would not care about the ",[131,3468,200],{}," at all, so we could simply subscribe to ",[131,3471,2193],{}," as a stream. However, since we know that we eventually want to create a new album and add it to the stream, it is better to use the ",[131,3474,200],{}," in this instance.",[225,3477,3479],{"id":3478},"streambuilder",[131,3480,3265],{},[116,3482,3483,3484,3486,3487,3489,3490,3492,3493,3495,3496,3498,3499,3501,3502,3505],{},"The next thing we need to do is update the ",[131,3485,2384],{}," widget. Previously, we were using a ",[131,3488,196],{}," widget and calling the ",[131,3491,2193],{}," method to get the data we needed to build the list. Since ",[131,3494,2193],{}," no longer returns a list of albums, this will not work for us anymore. Instead, we will use the ",[131,3497,3265],{}," widget to accomplish the task, and instead of a ",[131,3500,2189],{}," parameter, we have a ",[131,3503,3504],{},"stream"," parameter.",[658,3507,3508,3509,3511,3512,3517,3518,3520],{},"Where does the stream come from, you ask? The answer lies within our ",[131,3510,200],{},". By specifying ",[111,3513,3514],{},[131,3515,3516],{},"streamController.stream"," as the stream to use, any data emitted from the ",[131,3519,200],{}," will be used to build the list.",[116,3522,3523,3524,3526,3527,3529,3530,3532],{},"We are not quite done yet, though. The ",[131,3525,2457],{}," still wants a list of items to use to build itself. Before, that list was provided directly by the ",[131,3528,2193],{}," method. Now, we no longer have a list — we just have a stream of items. We are going to have to build and maintain our own list. We will prefix the variable with an underscore, just to remind ourselves that we are only using it within the context of this single widget, and we should not be trying to access it from anywhere else. We will also want to place it outside our ",[131,3531,2376],{}," method, so it does not get reinitialized to an empty list each time the widget rebuilds:",[233,3534,3537],{"className":235,"code":3535,"filename":3536,"language":238,"meta":106,"style":106},"...\nfinal List\u003CAlbum> _albums = [];\n\n@override\nWidget build(BuildContext context) {\n  ...\n","Example 7.26 · Example 7.26.dart",[131,3538,3539,3543,3548,3552,3557,3562],{"__ignoreMap":106},[242,3540,3541],{"class":244,"line":245},[242,3542,3252],{},[242,3544,3545],{"class":244,"line":251},[242,3546,3547],{},"final List\u003CAlbum> _albums = [];\n",[242,3549,3550],{"class":244,"line":257},[242,3551,261],{"emptyLinePlaceholder":260},[242,3553,3554],{"class":244,"line":264},[242,3555,3556],{},"@override\n",[242,3558,3559],{"class":244,"line":270},[242,3560,3561],{},"Widget build(BuildContext context) {\n",[242,3563,3564],{"class":244,"line":276},[242,3565,3566],{},"  ...\n",[116,3568,3569,3570,3573,3574,3576,3577,3580,3581,3584,3585,3587,3588,3590,3591,3594,3595,3597],{},"We will also want to convert our ",[131,3571,3572],{},"StatelessWidget"," into a ",[131,3575,2388],{},", so we can start the stream when the widget is initialized. Finally, we want to change the way we handle the incoming data. Instead of assigning ",[131,3578,3579],{},"snapshot.data"," to the ",[131,3582,3583],{},"albums"," variable, we want to add that ",[131,3586,3579],{}," (which is now a single ",[131,3589,1155],{},") to our new list of albums, ",[131,3592,3593],{},"_albums",". We can also remove our ",[131,3596,2331],{}," and clean up the logic which tests the connection state, since we no longer care what the connection state is and only if there is new data to add:",[233,3599,3602],{"className":235,"code":3600,"filename":3601,"language":238,"meta":106,"style":106},"class AlbumList extends StatefulWidget {\n  const AlbumList({super.key});\n\n  @override\n  _AlbumListState createState() => _AlbumListState();\n}\n\nclass _AlbumListState extends State\u003CAlbumList> {\n  final List\u003CAlbum> _albums = [];\n\n  @override\n  Widget build(BuildContext context) {\n    return StreamBuilder(\n      stream: streamController.stream,\n      builder: (BuildContext context, AsyncSnapshot snapshot) {\n        if (snapshot.hasData) {\n          _albums.add(snapshot.data);\n        }\n        return ListView.builder(\n          shrinkWrap: true,\n          itemCount: _albums.length,\n          itemBuilder: (BuildContext context, int index) {\n            return Card(\n              child: Padding(\n                padding: const EdgeInsets.all(8),\n                child: Text(_albums[index].title ?? \"Undefined\"),\n              ),\n            );\n          },\n        );\n      },\n    );\n  }\n\n  @override\n  void initState() {\n    super.initState();\n    fetchAlbums();\n  }\n}\n","Example 7.27 · Example 7.27.dart",[131,3603,3604,3609,3614,3618,3622,3627,3631,3635,3640,3645,3649,3653,3657,3662,3667,3671,3676,3681,3685,3690,3695,3700,3705,3710,3715,3720,3725,3730,3735,3740,3745,3749,3753,3757,3761,3765,3771,3777,3783,3788],{"__ignoreMap":106},[242,3605,3606],{"class":244,"line":245},[242,3607,3608],{},"class AlbumList extends StatefulWidget {\n",[242,3610,3611],{"class":244,"line":251},[242,3612,3613],{},"  const AlbumList({super.key});\n",[242,3615,3616],{"class":244,"line":257},[242,3617,261],{"emptyLinePlaceholder":260},[242,3619,3620],{"class":244,"line":264},[242,3621,1939],{},[242,3623,3624],{"class":244,"line":270},[242,3625,3626],{},"  _AlbumListState createState() => _AlbumListState();\n",[242,3628,3629],{"class":244,"line":276},[242,3630,296],{},[242,3632,3633],{"class":244,"line":282},[242,3634,261],{"emptyLinePlaceholder":260},[242,3636,3637],{"class":244,"line":287},[242,3638,3639],{},"class _AlbumListState extends State\u003CAlbumList> {\n",[242,3641,3642],{"class":244,"line":293},[242,3643,3644],{},"  final List\u003CAlbum> _albums = [];\n",[242,3646,3647],{"class":244,"line":299},[242,3648,261],{"emptyLinePlaceholder":260},[242,3650,3651],{"class":244,"line":304},[242,3652,1939],{},[242,3654,3655],{"class":244,"line":310},[242,3656,1944],{},[242,3658,3659],{"class":244,"line":316},[242,3660,3661],{},"    return StreamBuilder(\n",[242,3663,3664],{"class":244,"line":322},[242,3665,3666],{},"      stream: streamController.stream,\n",[242,3668,3669],{"class":244,"line":328},[242,3670,2247],{},[242,3672,3673],{"class":244,"line":902},[242,3674,3675],{},"        if (snapshot.hasData) {\n",[242,3677,3678],{"class":244,"line":1511},[242,3679,3680],{},"          _albums.add(snapshot.data);\n",[242,3682,3683],{"class":244,"line":1516},[242,3684,2272],{},[242,3686,3687],{"class":244,"line":2011},[242,3688,3689],{},"        return ListView.builder(\n",[242,3691,3692],{"class":244,"line":2016},[242,3693,3694],{},"          shrinkWrap: true,\n",[242,3696,3697],{"class":244,"line":2021},[242,3698,3699],{},"          itemCount: _albums.length,\n",[242,3701,3702],{"class":244,"line":2026},[242,3703,3704],{},"          itemBuilder: (BuildContext context, int index) {\n",[242,3706,3707],{"class":244,"line":2032},[242,3708,3709],{},"            return Card(\n",[242,3711,3712],{"class":244,"line":2037},[242,3713,3714],{},"              child: Padding(\n",[242,3716,3717],{"class":244,"line":2042},[242,3718,3719],{},"                padding: const EdgeInsets.all(8),\n",[242,3721,3722],{"class":244,"line":2048},[242,3723,3724],{},"                child: Text(_albums[index].title ?? \"Undefined\"),\n",[242,3726,3727],{"class":244,"line":2053},[242,3728,3729],{},"              ),\n",[242,3731,3732],{"class":244,"line":2733},[242,3733,3734],{},"            );\n",[242,3736,3737],{"class":244,"line":2739},[242,3738,3739],{},"          },\n",[242,3741,3742],{"class":244,"line":2745},[242,3743,3744],{},"        );\n",[242,3746,3747],{"class":244,"line":2751},[242,3748,2301],{},[242,3750,3751],{"class":244,"line":2756},[242,3752,2008],{},[242,3754,3755],{"class":244,"line":2762},[242,3756,881],{},[242,3758,3759],{"class":244,"line":2767},[242,3760,261],{"emptyLinePlaceholder":260},[242,3762,3763],{"class":244,"line":2772},[242,3764,1939],{},[242,3766,3768],{"class":244,"line":3767},36,[242,3769,3770],{},"  void initState() {\n",[242,3772,3774],{"class":244,"line":3773},37,[242,3775,3776],{},"    super.initState();\n",[242,3778,3780],{"class":244,"line":3779},38,[242,3781,3782],{},"    fetchAlbums();\n",[242,3784,3786],{"class":244,"line":3785},39,[242,3787,881],{},[242,3789,3791],{"class":244,"line":3790},40,[242,3792,296],{},[124,3794,3795,600,3797,3799,3800,3802,3803,3806,3807,3809],{},[111,3796,611],{},[131,3798,2197],{}," is the wrong place to add to ",[131,3801,3593],{},". The builder \"is called at the discretion of the Flutter pipeline, and will thus receive a timing-dependent sub-sequence of the snapshots\" (Flutter, n.d.-c). So every rebuild that is not caused by a new event (a hot reload, a parent rebuild) adds the last album again, and albums that arrive within the same frame are skipped. The runnable copy adds each album exactly once, when the stream emits it (",[131,3804,3805],{},"streamController.stream.map(...)",", created once in ",[131,3808,2392],{},"), and the builder only reads the list.",[116,3811,3812,3813,503],{},"Now, if you run the code, you will see the individual albums stream into the list, ",[111,3814,3815],{},"one by one",[225,3817,3819],{"id":3818},"adding-the-new-album-to-the-stream","Adding the new album to the stream",[116,3821,3822,3823,3825,3826,3828,3829,1082],{},"There is just one last thing we must do: when we create a new album, we need to add it to the list. Fortunately, this is the exact same process that we use in the ",[131,3824,2193],{}," method. In our ",[131,3827,2607],{}," widget, once we have created the new album, we simply add an event to the ",[131,3830,200],{},[233,3832,3835],{"className":235,"code":3833,"filename":3834,"language":238,"meta":106,"style":106},"class Home extends StatelessWidget {\n  const Home({super.key});\n\n  @override\n  Widget build(BuildContext context) {\n    return Scaffold(\n      appBar: AppBar(\n        title: const Text('Albums'),\n      ),\n      floatingActionButton: FloatingActionButton(\n        onPressed: () async {\n          final String? title = await _userInputDialog(context);\n          if (title == null) {\n            throw Exception('Cancelled Album creation.');\n          }\n\n          if (title.isNotEmpty) {\n            final Album album = await createAlbum(title);\n            streamController.sink.add(album);\n          }\n        },\n        child: const Icon(Icons.add),\n      ),\n      body: const AlbumList(),\n    );\n  }\n}\n","Example 7.28 · Example 7.28.dart",[131,3836,3837,3841,3845,3849,3853,3857,3861,3865,3869,3873,3877,3881,3885,3889,3893,3897,3901,3905,3909,3914,3918,3922,3926,3930,3934,3938,3942],{"__ignoreMap":106},[242,3838,3839],{"class":244,"line":245},[242,3840,2684],{},[242,3842,3843],{"class":244,"line":251},[242,3844,2689],{},[242,3846,3847],{"class":244,"line":257},[242,3848,261],{"emptyLinePlaceholder":260},[242,3850,3851],{"class":244,"line":264},[242,3852,1939],{},[242,3854,3855],{"class":244,"line":270},[242,3856,1944],{},[242,3858,3859],{"class":244,"line":276},[242,3860,2706],{},[242,3862,3863],{"class":244,"line":282},[242,3864,2711],{},[242,3866,3867],{"class":244,"line":287},[242,3868,2716],{},[242,3870,3871],{"class":244,"line":293},[242,3872,1974],{},[242,3874,3875],{"class":244,"line":299},[242,3876,2725],{},[242,3878,3879],{"class":244,"line":304},[242,3880,2730],{},[242,3882,3883],{"class":244,"line":310},[242,3884,3018],{},[242,3886,3887],{"class":244,"line":316},[242,3888,3023],{},[242,3890,3891],{"class":244,"line":322},[242,3892,3028],{},[242,3894,3895],{"class":244,"line":328},[242,3896,3033],{},[242,3898,3899],{"class":244,"line":902},[242,3900,261],{"emptyLinePlaceholder":260},[242,3902,3903],{"class":244,"line":1511},[242,3904,3042],{},[242,3906,3907],{"class":244,"line":1516},[242,3908,3047],{},[242,3910,3911],{"class":244,"line":2011},[242,3912,3913],{},"            streamController.sink.add(album);\n",[242,3915,3916],{"class":244,"line":2016},[242,3917,3033],{},[242,3919,3920],{"class":244,"line":2021},[242,3921,2742],{},[242,3923,3924],{"class":244,"line":2026},[242,3925,2748],{},[242,3927,3928],{"class":244,"line":2032},[242,3929,1974],{},[242,3931,3932],{"class":244,"line":2037},[242,3933,2759],{},[242,3935,3936],{"class":244,"line":2042},[242,3937,2008],{},[242,3939,3940],{"class":244,"line":2048},[242,3941,881],{},[242,3943,3944],{"class":244,"line":2053},[242,3945,296],{},[116,3947,3948,336,3950,2571,3953,3955],{},[220,3949,335],{},[131,3951,3952],{},"examples\u002Fflutter_for_jobseekers_ch07\u002Flib\u002Fmain.dart",[131,3954,200],{}," app; cancelling the dialog returns instead of throwing an uncaught exception)",[658,3957,3958,3959,3962],{},"Now, whenever you create a new album, it will be ",[111,3960,3961],{},"added to the bottom of the list, automatically","!",[150,3964,3966],{"id":3965},"conclusion","Conclusion",[116,3968,3969,3970,593,3972,3974,3975,593,3977,3979,3980,3982],{},"Congratulations on making it through this chapter. Some of the content we covered here was relatively complex. We learned all about concurrency, including the ",[131,3971,485],{},[131,3973,489],{}," keywords, ",[131,3976,3132],{},[131,3978,3135],{}," keywords, and using a ",[131,3981,200],{}," to add an event to the sink and listen to the objects being emitted from the stream, along with their corresponding widgets. We also learned both how to receive data from and send data to an API. Then, we learned how to convert the raw data we received from the API into a Dart object that we care about.",[658,3984,3985,3986,3989],{},"Armed with this knowledge, you will have all the tools you need to build ",[111,3987,3988],{},"dynamic applications that leverage the power of APIs"," — opening a whole new world of possibilities! What will you build first? Maybe a weather app?",[116,3991,3992,3993,3996,3997,3999,4000,4003],{},"In the upcoming chapter, we are going to explore just what exactly we were doing when we invoked ",[131,3994,3995],{},"Navigator.pop()"," in our ",[131,3998,2792],{},". In fact, we will take an in-depth look at what exactly ",[131,4001,4002],{},"Navigator"," is, how it works, and what its shortcomings are. Then, we will look at the newer API that unlocks even more potential, and finally we will explore a package that helps make our life easier when we are working with navigation and routing. See you over there!",[150,4005,4007],{"id":4006},"questions","Questions",[4009,4010,4011,4017,4020,4023,4031,4034,4037,4040,4043,4046,4049,4052,4069,4072,4075,4078,4085,4091,4104,4107,4117,4120,4127,4135,4138],"ol",{},[166,4012,4013,4014,4016],{},"What is a ",[131,4015,214],{},"?",[166,4018,4019],{},"What is the difference between synchronous code and asynchronous code?",[166,4021,4022],{},"What happens to the application when there is a long-lived, synchronous task being executed?",[166,4024,4025,4026,486,4028,4030],{},"What are the ",[131,4027,485],{},[131,4029,489],{}," keywords used for?",[166,4032,4033],{},"What type of value does an asynchronous operation return?",[166,4035,4036],{},"What is an API?",[166,4038,4039],{},"What package(s) can be used to communicate with an API?",[166,4041,4042],{},"What sort of data is generally returned from an API?",[166,4044,4045],{},"What is an HTTP status code, and what are some common ones?",[166,4047,4048],{},"What is a guard clause?",[166,4050,4051],{},"What is JSON, and what are some considerations which are important to consider when working with JSON data?",[166,4053,4054,4055,486,4057,4059,4060,1758,4063,1765,4066,4016],{},"When communicating with an API using an HTTP client, a \"verb\" is used to determine which kind of action the client should take. The verbs ",[131,4056,1695],{},[131,4058,1699],{}," are used for which types of operations? Extra credit: What about ",[131,4061,4062],{},"PUT",[131,4064,4065],{},"PATCH",[131,4067,4068],{},"DELETE",[166,4070,4071],{},"When sending a POST request to an API, which properties are required to be passed into the request?",[166,4073,4074],{},"What does CRUD mean?",[166,4076,4077],{},"What HTTP verbs relate to which CRUD operations?",[166,4079,4080,4081,4084],{},"How can you generate ",[131,4082,4083],{},"List\u003Cint>"," containing all the odd numbers from 27 to 99?",[166,4086,4087,4088,4090],{},"How can you tell if a ",[131,4089,196],{}," has completed its future?",[166,4092,4093,4094,4096,4097,4099,4100,4096,4102,4016],{},"Why would somebody choose to use a ",[131,4095,2095],{}," over a ",[131,4098,2465],{},"? Why would somebody choose to use a ",[131,4101,2465],{},[131,4103,2095],{},[166,4105,4106],{},"How do you display a dialog to a user?",[166,4108,4109,4110,4112,4113,4116],{},"Given a dialog with two options, yes and no, how do you return either ",[131,4111,2070],{}," or ",[131,4114,4115],{},"false"," from the dialog?",[166,4118,4119],{},"What is a sink? What is a stream?",[166,4121,4025,4122,486,4124,4126],{},[131,4123,3132],{},[131,4125,3135],{}," operators used for?",[166,4128,4129,4130,4132,4133,4016],{},"What is the ",[131,4131,3132],{}," equivalent to a ",[131,4134,196],{},[166,4136,4137],{},"What library provides widgets which are used with sinks and streams?",[166,4139,4140],{},"How do you add an event to a sink?",[150,4142,4144],{"id":4143},"key-terms","Key terms",[753,4146,4147,4151,4157,4166,4170,4178,4181,4185,4198,4205],{},[756,4148,4150],{"name":4149},"Application Programming Interface (API)","A connection between two pieces of software to exchange information.",[756,4152,4153,4154,503],{"name":485},"Dart's async keyword is used to mark a method as having a return type of ",[131,4155,4156],{},"Future\u003CT>",[756,4158,4159,4160,4162,4163,503],{"name":3132},"Dart's ",[131,4161,3132],{}," keyword is used to mark a method as having a return value of ",[131,4164,4165],{},"Stream\u003CT>",[756,4167,4169],{"name":4168},"Asynchronous operations","Code that executes in parallel, neither blocking the other.",[756,4171,4159,4172,4174,4175,4177],{"name":489},[131,4173,489],{}," command is used to wait for the execution of an asynchronous operation to complete its future. The ",[131,4176,489],{}," keyword must exist within an asynchronous method or widget which expects a future.",[756,4179,4180],{"name":214},"A promise given by an asynchronous operation to return a value when it has finished processing.",[756,4182,4184],{"name":4183},"JSON","JavaScript Object Notation. A key\u002Fvalue pair-based textual representation of data. JSON is commonly used when sending data to and receiving data from APIs.",[756,4186,4187,4188,4190,4191,4193,4194,4197],{"name":359},"Code that is executed whenever a ",[131,4189,1144],{}," is evaluated during a widget build or method call. A null string being interpolated will return the string \"",[131,4192,363],{},"\" while methods being called will run at the time that the string is evaluated. Code that is interpolated is contained within ",[131,4195,4196],{},"${}"," in a string.",[756,4199,4201,4202,503],{"name":4200},"Synchronous operations","Code that executes one after the other. The next line cannot execute until the previous has finished, which is ",[220,4203,4204],{},"blocking",[756,4206,4159,4207,4209,4210,4212],{"name":3135},[131,4208,3135],{}," keyword is used inside an ",[131,4211,3132],{}," method to add an item to the stream.",[124,4214,4215,4217,4218,486,4220,4223,4224,4227],{},[111,4216,611],{}," asynchronous Dart code does not run in parallel. An isolate has \"a single thread running an event loop\"; ",[131,4219,214],{},[131,4221,4222],{},"Stream"," code takes turns on that one thread, and only separate isolates \"enable parallel code execution on multiple processor cores\" (Dart, n.d.-b). Asynchronous operations are ",[220,4225,4226],{},"concurrent",": while one waits, for example on file I\u002FO, other code runs.",[150,4229,4231],{"id":4230},"further-reading","Further reading",[155,4233,4234,4241],{},[158,4235,4240],{"icon":4236,"title":4237,"target":4238,"to":4239},"i-simple-icons-flutter","JSON and serialization","_blank","https:\u002F\u002Fdocs.flutter.dev\u002Fdata-and-backend\u002Fserialization\u002Fjson","Learn more about JSON and serialization.",[158,4242,4246],{"icon":4243,"title":4244,"target":4238,"to":4245},"i-simple-icons-mdnwebdocs","HTTP response status codes","https:\u002F\u002Fdeveloper.mozilla.org\u002Fen-US\u002Fdocs\u002FWeb\u002FHTTP\u002FStatus","HTTP status codes and their meanings.",[124,4248,4249,4251,4252,4256,4257,4260],{},[111,4250,611],{}," the book's link, ",[140,4253,4254],{"href":4254,"rel":4255},"https:\u002F\u002Fdocs.flutter.dev\u002Fdevelopment\u002Fdata-and-backend\u002Fjson",[144],", now redirects to ",[140,4258,4239],{"href":4239,"rel":4259},[144]," (Flutter, n.d.-d).",[150,4262,4264],{"id":4263},"references","References",[163,4266,4267,4280,4293,4305,4317,4330,4343,4355,4367,4378],{},[166,4268,4269,4270,4272,4273,336,4277],{},"BPB Publications. (n.d.). ",[220,4271,17],{}," (companion code repository). Retrieved from GitHub: ",[140,4274,4275],{"href":4275,"rel":4276},"https:\u002F\u002Fgithub.com\u002Fbpbpublications\u002FFlutter-for-Jobseekers",[144],[220,4278,4279],{},"(added, not in the book)",[166,4281,4282,4283,4286,4287,336,4291],{},"Dart. (n.d.-a). ",[220,4284,4285],{},"Asynchronous programming",". Retrieved from Dart: ",[140,4288,4289],{"href":4289,"rel":4290},"https:\u002F\u002Fdart.dev\u002Flanguage\u002Fasync",[144],[220,4292,4279],{},[166,4294,4295,4296,4286,4299,336,4303],{},"Dart. (n.d.-b). ",[220,4297,4298],{},"Concurrency in Dart",[140,4300,4301],{"href":4301,"rel":4302},"https:\u002F\u002Fdart.dev\u002Flanguage\u002Fconcurrency",[144],[220,4304,4279],{},[166,4306,4307,4308,4286,4311,336,4315],{},"Dart. (n.d.-c). ",[220,4309,4310],{},"Libraries & imports",[140,4312,4313],{"href":4313,"rel":4314},"https:\u002F\u002Fdart.dev\u002Flanguage\u002Flibraries",[144],[220,4316,4279],{},[166,4318,4319,4320,4323,4324,336,4328],{},"Dart. (n.d.-d). ",[220,4321,4322],{},"StreamController class",". Retrieved from Dart API: ",[140,4325,4326],{"href":4326,"rel":4327},"https:\u002F\u002Fapi.dart.dev\u002Fdart-async\u002FStreamController-class.html",[144],[220,4329,4279],{},[166,4331,4332,4333,4336,4337,336,4341],{},"Flutter. (n.d.-a). ",[220,4334,4335],{},"useMaterial3 property",". Retrieved from Flutter API: ",[140,4338,4339],{"href":4339,"rel":4340},"https:\u002F\u002Fapi.flutter.dev\u002Fflutter\u002Fmaterial\u002FThemeData\u002FuseMaterial3.html",[144],[220,4342,4279],{},[166,4344,4345,4346,4336,4349,336,4353],{},"Flutter. (n.d.-b). ",[220,4347,4348],{},"FutureBuilder class",[140,4350,4351],{"href":4351,"rel":4352},"https:\u002F\u002Fapi.flutter.dev\u002Fflutter\u002Fwidgets\u002FFutureBuilder-class.html",[144],[220,4354,4279],{},[166,4356,4357,4358,4336,4361,336,4365],{},"Flutter. (n.d.-c). ",[220,4359,4360],{},"StreamBuilder class",[140,4362,4363],{"href":4363,"rel":4364},"https:\u002F\u002Fapi.flutter.dev\u002Fflutter\u002Fwidgets\u002FStreamBuilder-class.html",[144],[220,4366,4279],{},[166,4368,4369,4370,4372,4373,336,4376],{},"Flutter. (n.d.-d). ",[220,4371,4237],{},". Retrieved from Flutter: ",[140,4374,4239],{"href":4239,"rel":4375},[144],[220,4377,4279],{},[166,4379,4380,4381,4384,4385,336,4389],{},"JSONPlaceholder. (n.d.). ",[220,4382,4383],{},"Guide",". Retrieved from JSONPlaceholder: ",[140,4386,4387],{"href":4387,"rel":4388},"https:\u002F\u002Fjsonplaceholder.typicode.com\u002Fguide\u002F",[144],[220,4390,4279],{},[4392,4393,4394],"style",{},"html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sMK4o, html code.shiki .sMK4o{--shiki-light:#39ADB5;--shiki-default:#89DDFF;--shiki-dark:#89DDFF}html pre.shiki code .spNyl, html code.shiki .spNyl{--shiki-light:#9C3EDA;--shiki-default:#C792EA;--shiki-dark:#C792EA}html pre.shiki code .sbssI, html code.shiki .sbssI{--shiki-light:#F76D47;--shiki-default:#F78C6C;--shiki-dark:#F78C6C}html pre.shiki code .sTEyZ, html code.shiki .sTEyZ{--shiki-light:#90A4AE;--shiki-default:#EEFFFF;--shiki-dark:#BABED8}html pre.shiki code .sfazB, html code.shiki .sfazB{--shiki-light:#91B859;--shiki-default:#C3E88D;--shiki-dark:#C3E88D}",{"title":106,"searchDepth":251,"depth":251,"links":4396},[4397,4398,4404,4411,4419,4422,4429,4437,4438,4439,4440,4441],{"id":152,"depth":251,"text":153},{"id":208,"depth":251,"text":168,"children":4399},[4400,4401,4402],{"id":227,"depth":257,"text":228},{"id":392,"depth":257,"text":393},{"id":543,"depth":257,"text":4403},"Using .then()",{"id":678,"depth":251,"text":171,"children":4405},[4406,4407,4409,4410],{"id":719,"depth":257,"text":720},{"id":784,"depth":257,"text":4408},"Instantiating a Client per call",{"id":810,"depth":257,"text":811},{"id":914,"depth":257,"text":915},{"id":1021,"depth":251,"text":174,"children":4412},[4413,4414,4416,4418],{"id":1031,"depth":257,"text":1032},{"id":1152,"depth":257,"text":4415},"The Album class",{"id":1264,"depth":257,"text":4417},"Returning an Album",{"id":1421,"depth":257,"text":1422},{"id":1580,"depth":251,"text":177,"children":4420},[4421],{"id":1815,"depth":257,"text":1816},{"id":1903,"depth":251,"text":180,"children":4423},[4424,4425,4426,4428],{"id":2085,"depth":257,"text":196},{"id":2395,"depth":257,"text":2095},{"id":2580,"depth":257,"text":4427},"Adding a FloatingActionButton",{"id":2777,"depth":257,"text":2778},{"id":3121,"depth":251,"text":183,"children":4430},[4431,4433,4435,4436],{"id":3191,"depth":257,"text":4432},"The StreamController",{"id":3357,"depth":257,"text":4434},"The async* alternative",{"id":3478,"depth":257,"text":3265},{"id":3818,"depth":257,"text":3819},{"id":3965,"depth":251,"text":3966},{"id":4006,"depth":251,"text":4007},{"id":4143,"depth":251,"text":4144},{"id":4230,"depth":251,"text":4231},{"id":4263,"depth":251,"text":4264},"Futures and async\u002Fawait, requesting and sending data with the http package, turning JSON into Dart classes, FutureBuilder and StreamBuilder, and sinks and streams.","md",null,{},{"icon":56},{"title":53,"description":4442},"CIDOn8mNwHmdWBkemQCZZL0z-P6v1-kr9co9uAGTsgQ",[4450,4452],{"title":48,"path":49,"stem":50,"description":4451,"icon":51,"children":-1},"The pubspec.yaml file, adding images and fonts as assets, the Pub package ecosystem, judging package quality, playing audio with a package, and popular packages.",{"title":58,"path":59,"stem":60,"description":4453,"icon":61,"children":-1},"Navigator 1.0 (push and pop, named routes, route arguments, returning data, Hero animations), the Router API (Navigator 2.0), and routing with Beamer.",1795696849894]