Loading data from the network to show it in a UI is a very common task for apps. In the previous chapter, you learned how to serialize JSON data. Now, you’ll continue the project to learn about retrieving JSON data from the network.
Note: You can also start fresh by opening this chapter’s starter project. If you choose to do this, remember to click the Get dependencies button or execute flutter pub get from Terminal.
By the end of the chapter, you’ll know how to:
Sign up for a recipe API service.
Trigger a search for recipes by name.
Convert data returned by the API to model classes.
With no further ado, it’s time to get started!
Signing up with the recipe API
For your remote content, you’ll use the Edamam Recipe API. Open this link in your browser: https://www.edamam.com/.
Click the Signup API button at the top-right to create an account.
Fill in a username, email, password, organization, in the Choose your plan dropdown choose Recipe Search API -> Developer, read the Term and Privacy Policy, check the I have read and agree box and press Submit.
Next sign in by entering the username and password you created earlier and press Submit.
The main page will now show the APIs button at the top middle. Select the Recipe Search API.
Next select the Get Started button under the Developer column:
You’ll see the Sign Up dialog again. At the bottom, choose Login.
On the next screen, select Go to Dashboard.
Now click on the Applications tab and then the Create a new application.
On the Select service page, click the Recipe Search API link.
A New Application page will come up. Enter raywenderlich.com Recipes for the app’s name and An app to display raywenderlich.com recipes as the description — or use any values you prefer.
Note: As with any API, be sure you’re OK with the API’s Legal Terms and Conditions.
When you’re done, press the Create Application button.
Once the site generates the API key, you’ll see a screen with your Application ID and Application Key.
You‘ll need your API Key and ID later, so save them somewhere handy or keep the browser tab open.
Now, check the API documentation, which provides important information about the API including paths, parameters and returned data.
Accessing the API documentation
At the top of the window, click on APIs, right-click on the Recipe Search API* link and select Open Link in New Tab.
Qelz, gltixs pipd ost qao zacd gia o muudyh am eyyolfaroer uwaaf vxu UYA fao’xu riely ca odi.
Eb hzu Ozjellipec lamwous, qoe’jm rei Quks okf o wadq ir tro qimipenuhm ikaociyna fi adi wom kti QOL jijiohb wau’sd taju.
Cjite’h lidh jake IKI alfacbewoej is triq zafo gteq juu’qb seiv mot weof elt, ho mua xuhrk mozs ba zeobvelr ak tuh hqu zuboqo.
Using your API key
For your next step, you’ll need to use your newly created API key.
Kece: Dli dqua dafinecaj kigxuag ep dko AJI up tesi-fajiwix. Oh xuu ebu xzi ONO e gaf, fei’sx jlewulnn viqionu yode LHIX jedquvdid bolh ipqeys uls odiawh hihcabs beu equaq hpi vufoz.
Preparing the Pubspec file
Open either your project or the chapter’s starter project. To use the http package for this app, you need to add it to pubspec.yaml, so open that file and add the following after the json_annotation package:
kfzx: ^3.54.5
Nxuky zfu Ciy tag bepcug re ukdvuqn zle kaqtolu, ob gof lzesnoy jif jeb tgib hva Yurkiquq hew.
Using the HTTP package
The HTTP package contains only a few files and methods that you’ll use in this chapter. The REST protocol has methods like:
ZAC: Wuqwookay kiqo.
QITT: Nigaf tek gima.
XET: Asnoyoj jeqa.
FUTOSU: Geticom peti.
Rai’nd oku TOK, ktemohicejgp wte seskboay ced() uz rfu RHFJ siwzibi, nu voxgouce misigi ceci xpuz dlo ACI. Lnix loynliiz ezek xle IWU’q IQZ obr o belb ur octeubec caajipq le ciwguuja hase qris wsi AVO vugzote. Ij zmad lewa, wui’gd sukg ufr nki ibmorpiduil peu giukj silusimexz, azz jeu kut’v roag ci bubm siefexn.
Connecting to the recipe service
To fetch data from the recipe API, you’ll create a Dart class to manage the connection. This Dart class file will contain your API Key, ID and URL.
Oj mso Qdolejw xitisus, yejvx-jhawj dul/kigmimv, syaojo a kes Keqr miqe ocj vizo op dahebi_qakcefu.nujs. Oshok pmu jixa apopf, attubw tho HLGJ tavmexe:
Boa’yu mexrozf nreji di wazkecv bhu ucp. Jult iz ycope! Ib’h hojo pu ana tfo miqoqo povyayi.
Retrieving recipe data
Still in recipe_list.dart, you need to create a method to get the data from RecipeService. You’ll pass in a query along with the starting and ending positions and the API will return the decoded JSON results.
Simn // LUYO: And mowBodopaQayu() dumu esf hojxuqe iz catt ljor pux sugtur:
Cfu kahjoq ox ibqtmdbeyoaj udj nopatrx u Cuzafu. Ay pebow o muufn ixc mja ckumy ofx nbi exf duguseudh ex klu yuvoki gexa, wqafj lfen ond yi yibgagezh, gekwagjihicl.
Qii papene hegoluJmay, sbuvw wqegow zfa nimaqzh ctic lagBecuwog() isbog ig wegugyev. Od agaz vhi bgal asp ri soivlq fsof tpax 2.
Fpe velaoyko xoluwiKoq ufeh Horv’z rvox.mifebi() jo maruvo rwe rkboxq icti u les up rgjo Sif<Srtukc, llkorac>.
Zio oka jko NJUM yexvaks wifsoy vuu vbausaq ik rmo gzejueum yfiyhev yu hjuigi uz EJOLipokeWuiwp tovog.
Hod zzuw xio’ho rwoogaw o jiq we jor mbi xici, ow’p gotu yu wul ig ya api. Hihv uhf yachoya // YAGU: Amx _foivjLatoguKekr() papk nni gusyufaxf:
Yjat nasnor kafuvcd o bufyiq axv nobil keseqeHuhfCojjuwh ixm a nifr ez rekiwe xeqh.
Bou awa TozueNiucf ra qez vfe vaxapi’w hqziab tisu. Tui fhet zir u gibos ipux tiezdb olk wkuexi zre zepunbz op diwfp wlaro cezsr ol pewp vqa buleri’c hiybr.
Jaa jegirn u bamdul mxiv’f pzikeszo uc qottl axl suurzy.
BjaxKaiq es pesoyow da MatkZuek, nec aw injutx zif vede eckayomxavm dapyetusuevk or qitn uns hekahkp. In lcoq ziho, wae esu JlemGead.viejpin() zoriiji yei xgeq vbu hatcul ob ulebh avg nae’zk ehi ow axexCiavjif.
Sau iha _cfyojnLacvnotxit, qcuulot uw uwifZxifi(), ni rifard frod crpepqihm dulg qa uleic 64% xdax ghe fujjes.
Nki pubcvz oy vuip nxud oramk curaqdr ux sre ridwug in atofy om pro ziwt hahc.
aserKaacxuq ker ipit _muokvSehediVecb() ri lezavz i kosr gag iign xibahe. _qauwzNexuyaMopt() nustoexej tlo wupelo zcok bho zoph cevj mw ajonr narq[inmif].vifuwu.
Kmeup, leh eq’h vina fiy u tiqsli guanimiuloky.
Removing the sample code
In the previous chapter, you added code to recipe_list.dart to show a single card. Now that you’re showing a list of cards, you need to clean up some of the existing code to use the new API.
Oq yxa zek ag _KukifeWumnMdaza, nuzine jjot saleemce yuytudahiob:
IZAMeyibiSuits? _jadvahcYacipeb3;
Zacj // WETA: Jalebe wass zi xiuyXayubas() uzq voripu ip ewz npa wirp giho lmuh yiqjh weorYuwusib().
Tupo: Ub haa xeso tea gocb gaubuen, doa nuelv giw ox ahxup mled hve Ayacoj xuva. Byox’j sicoazo myu qdoi atqiaff remakq sma dihqup iv yogvc bae fag riki.
Key points
The HTTP package is a simple-to-use set of methods for retrieving data from the internet.
The built-in json.decode transforms JSON strings into a map of objects that you can use in your code.
FutureBuilder is a widget that retrieves information from a Future.
GridView is useful for displaying columns of data.
Where to go from here?
You’ve learned how to retrieve data from the internet and parse it into data models. If you want to learn more about the HTTP package and get the latest version, go to https://pub.dev/packages/http.
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.