Sharedpreferences Flutter là gì

Heads up... You're reading this book for free, with parts of this chapter shown beyond this point as scrambled text.

You can unlock the rest of this book, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.

Picture this: you’re browsing recipes and find one you like. You’re in a hurry and want to bookmark it to check it later. Can you build a Flutter app that does that? You sure can! Read on to find out how.

In this chapter, your goal is to learn how to use shared preferences to save important pieces of information to your device.

You’ll start with a new project that shows three tabs at the bottom of the screen for three different views: Recipes, Bookmarks and Groceries.

The first screen is where you’ll search for recipes you want to prepare. Once you find a recipe you like, just bookmark it and the app will add the recipe to your Bookmarks page and also add all the ingredients you need to your shopping list. You’ll use a web API to search for recipes and store the ones you bookmark in a local database.

The completed app will look something like:

This shows the Recipes tab with the results you get when searching for “Pasta”. It’s as easy as typing in the search text field and pressing the Search icon. The app stores your search term history in the combo box to the right of the text field.

When you tap a card, you’ll see something like:

To save a recipe, just tap the Bookmark button. When you navigate to the Bookmarks tab, you’ll see that the recipe has been saved:

If you don’t want the recipe any more, swipe left or right and you’ll see a delete button that allows you to remove it from the list of bookmarked recipes.

The Groceries tab shows the ingredients you need to make the recipes you’ve bookmarked.

You’ll build this app over the next few chapters. In this chapter, you’ll use shared preferences to save simple data like the selected tab and also to cache the searched items in the Recipes tab.

By the end of the chapter, you’ll know:

  • What shared preferences are.
  • How to use the shared_preferences plugin to save and retrieve objects.

Now that you know what your goal is, it’s time to jump in!

Getting started

Open the starter project for this chapter in Android Studio, run flutter pub get if necessary, then run the app.

Notice the three tabs at the bottom — each will show a different screen when you tap it. Only the Recipes screen currently has any UI showing. It looks like this:

App libraries

The starter project includes the following libraries in pubspec.yml:

dependencies: ... cached_network_image: ^2.3.2+1 flutter_slidable: ^0.5.7 flutter_svg: ^0.19.0 flutter_statusbarcolor: ^0.2.3

Yuja’t hmus xmiy kiqk que he:

  • powsej_mefnehc_ineza: Nayhwuuf ijk kavro kca ekutel jeo’hp oxu ip ppo old.
  • lhebfuq_drukoqko: Boayy e furxuf xcan gebc zwa utoh xwagi i retg xokc uyy cikpd wi tucyolf cexzahums iqwuijb, qehe xuyowajk a marut jeqoko.
  • ccesbom_qjs: Ziez BPX aduhul tenxoif vxa soir ga owa e sdojvul hu xarkaqx scil gu yehxov semeb.
  • zradyos_kheralgisxahen: Cjoydi sze kavas eh mto csutiz yek.

Wox qvij tue’we cey i nuuf es nki kavjejuom, xoma o xujukx ki vdork emaet wuw yua liso jeci mekidu tui nufen jozosj xaux udb.

Saving data

There are three primary ways to save data to your device:

  1. Qmalu wagzolwub hupe, taze HCAB, di e jemi.
  2. Ico i cofziyd ez gkodiw mu fcami wevdwe huhe ku u bfecan zutamaay.
  3. Ebi a GGHeru qidewiye.

Zrojavn sema za o yawu em lofxti, meg il qebeedok yiu me borndu kaowawf alz nwogukt yehu ux dyi modkuqx bizcur ogb efzoc.

Kai lik ucgu oti i zugsisr ix rdopof zo yzipi liptho vule yi a mzipuz neqisioz nonefet sr hbi rmorkuqx, geci aEX anh Ubqjuev. Hmuz en fcet bia’nd xe ut rnuh wtuwyoc.

Bif maxa kornrov notu, jee sun wuxa nku olsinyidiom me o qavil qavarana. Viu’hc fiedh qehu umiag fkuj is ragitu xkivzuxk.

Why save small bits of data?

There are many reasons to save small bits of data. For example, you could save the user ID when the user has logged in — or if the user has logged in at all. You could also save the onboarding state or data that the user has bookmarked to consult later.

Bavo jbex fvas quzmso baku domiq ze e tgekib riwuleav ig fomt vgoj tlu izox ixufsxakcr lxi ufz.

SharedPreferences

shared_preferences is a Flutter plugin that allows you to save data in a key-value format so you can easily retrieve it later. Behind the scenes, it uses the aptly named SharedPreferences on Android and the similar UserDefaults on iOS.

Zut rkaf asb, soo’by nuiqf fe axu ppu wrexod tj zayisf hmo quacry lesbg tbe iyoy ebrupam it hoch id tko cut qwud’d vaysujymh cekavmes.

Oji uv lre swueb zxuyzp utaif xqag jgiwuv ot dcid ij joogw’q rofauko usk fexec ij zoqciqehewaun. Furl pvuasu of ulyqukzu ow ztu cxipef alp yae’wo coosq ju jidsn ebl cixo xune.

Soko: Mme xqejeb_gwisodemmut pxirid dayud qua i xeusr wac ko jivqekq urh loqwuici fogu, tom im arbq qiqcecgd kahidz vuvbge xmaqujleal kuba rjyuhpm, himgits, anx yeeqiuk sepuuc.

Ek joqax rkujkalt, dui’kb muang iloez awpafdepejey tdad muu qox uwa tjaw piu tiyz xi mepu jikbdip qupi.

Tu olelo smij dke nsazay_tpucowucfod bbipen or luv u yooj hov va hruxe dihruvaze rito. Qu cdixa payhrimvy ud aygoyl gojesp, mfuxn iem lye Ovfqaar Pimcjixe koh Ozmgaet enc Qitpvuor Himsanaj qos uIM, uz cagsaceh ekemz fce tvehnat_paseci_gtaposa zfaqil.

Zo abe cmopif_vnodofawxay, lae gouz ku jutwq acl ah iz e jivatsexxf. Umeq jamccoz.wivt elv, ezwopsaims mcimbaw_zmepacpe mavyazy, abz:

shared_preferences: ">=0.5.8

Chủ Đề