Get AI summaries of any video or article — Sign up free
Dictionary blocks in MIT App Inventor | Customer Record App | Storing records in MIT App Inventor thumbnail

Dictionary blocks in MIT App Inventor | Customer Record App | Storing records in MIT App Inventor

Obsidian Soft·
5 min read

Based on Obsidian Soft's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.

TL;DR

Dictionary blocks store data as key–value pairs, enabling reliable retrieval by referencing keys like “name” and “phone.”

Briefing

Dictionary blocks in MIT App Inventor are a practical way to store customer-style data as key–value pairs, then retrieve or display specific fields by referencing their keys. Instead of keeping separate variables for every attribute, a dictionary bundles related information into one structured record—like using “name” as the key to fetch the customer’s name value, and “phone” as the key to fetch the phone number. That structure matters because it scales cleanly from one customer to many: a single dictionary can represent one person, while a list of dictionaries can represent an entire customer database.

The walkthrough builds a “Customer Record” app for a departmental store. The interface uses two main screens (a main screen and an update screen), with a vertical layout containing labeled text boxes for “name” and “phone number,” plus buttons for “add,” “find,” “reload,” and “delete all.” A ListView sits below the buttons to display stored customers. Input validation is applied to the phone text box so only numbers are accepted.

On the data side, the app starts with global variables: one global dictionary called record (initialized as empty) and one global list called list of Records (initialized as empty). When the user presses the add button, the app constructs a new dictionary using the “make dictionary” block. The dictionary’s keys are taken from the text fields’ labels—such as “name”—and the values come directly from the user’s input (the text entered into the name and phone text boxes). That dictionary is then appended to the global list of Records using an “add item” block.

To persist data across app sessions, the list of dictionaries is stored in TinyDB under a tag named “customer data.” The app uses TinyDB’s “store value” procedure to save the entire list at once, which is crucial because the customer system needs multiple records, not just one.

Displaying the stored customers requires a dedicated procedure, update list view. Inside it, a local variable list is created as an empty list for building ListView elements. A for-each loop iterates through the global list of Records (each element is a dictionary). For each dictionary, the app creates a display string by joining the dictionary’s “name” value and “phone” value with a separator like “ - ”. Once the loop finishes, the procedure assigns the constructed list to ListView elements.

The app also loads existing data on startup. During the screen initialize event, it calls TinyDB’s “get value” using the same tag (“customer data”), ensuring that if the tag doesn’t exist, the fallback value is an empty list (not empty text). Finally, the delete all button clears everything: it uses TinyDB’s “clear all,” resets the global list of Records to an empty list, and empties ListView elements. The result is a working pattern for storing, listing, and managing multiple dictionary-based records in MIT App Inventor using TinyDB.

Cornell Notes

Dictionary blocks let MIT App Inventor store structured data as key–value pairs, such as “name” → customer name and “phone” → customer phone number. A single dictionary represents one customer record, while a list of dictionaries represents many customers. The app builds a global empty dictionary (record) and a global empty list (list of Records), then creates a new dictionary on “add,” appends it to the list, and saves the list to TinyDB under the tag “customer data.” A separate update list view procedure uses a for-each loop to turn each dictionary into a display string like “Name - Phone” and assigns the result to ListView elements. On startup, TinyDB data is loaded back into the list, and “delete all” clears both TinyDB and the UI.

How does a dictionary block represent a customer record, and what are the keys and values in this app?

Each customer record is built with a “make dictionary” block. The keys are fixed text labels like “name” and “phone,” and the values come from the user’s input fields (the text inside the name and phone text boxes). For example, the dictionary stores something like: key “name” → value from name text box; key “phone” → value from phone text box. This lets the app retrieve specific fields later by referencing the same keys.

Why does the app use a list of dictionaries instead of a single dictionary?

A single dictionary can hold only one customer’s fields at a time. The app needs multiple customers, so it maintains a global list called list of Records where each item is a dictionary. When the add button is pressed, the newly created dictionary is appended to this list using an “add item” block, allowing the database to grow record by record.

What does TinyDB store in this design, and why store the whole list at once?

TinyDB stores the entire list of dictionaries under the tag “customer data.” The “store value” procedure saves list of Records as the value. Storing the whole list at once keeps the data model consistent: when the app loads, it can retrieve one value (the list) and immediately iterate through it to rebuild the ListView.

How does the update list view procedure turn dictionary records into ListView entries?

update list view creates a local list variable (empty) to build display items. A for-each loop iterates through the global list of Records. For each dictionary, it uses “get value for key in dictionary” to fetch the “name” and “phone” values, then joins them into a single string with a separator like “ - ”. After the loop completes, the procedure assigns the constructed list to ListView elements. The local variable must stay inside the procedure’s scope, so ListView elements are set after the loop finishes.

What safeguards matter when loading from TinyDB on app startup?

On screen initialize, the app calls TinyDB’s “get value” using the exact same tag “customer data” used during storage. If the tag doesn’t exist, the fallback must be an empty list (because the program expects a list of dictionaries). Using empty text instead would break the later for-each loop that expects list items.

What exactly happens when the delete all button is pressed?

The delete all button clears persistent storage and resets the UI state. It calls TinyDB’s “clear all,” sets the global list of Records to an empty list, and sets ListView elements to an empty list as well. This ensures both the saved data and the displayed customer entries are removed together.

Review Questions

  1. In this app’s data model, what is the difference between the global dictionary record and the global list of Records?
  2. Why must the fallback value in TinyDB get value be an empty list (not empty text) when the tag is missing?
  3. Describe how update list view uses keys in dictionaries to build the strings shown in the ListView.

Key Points

  1. 1

    Dictionary blocks store data as key–value pairs, enabling reliable retrieval by referencing keys like “name” and “phone.”

  2. 2

    A single dictionary represents one customer record, while a list of dictionaries represents many customers.

  3. 3

    On “add,” the app builds a dictionary from text box inputs, then appends it to the global list of Records.

  4. 4

    TinyDB persists the entire list of dictionaries under the tag “customer data,” making reload straightforward.

  5. 5

    A dedicated update list view procedure uses a for-each loop to convert each dictionary record into a display string for ListView elements.

  6. 6

    Local variables inside update list view can’t be accessed outside the procedure; ListView elements must be set after the loop finishes.

  7. 7

    Delete all must clear TinyDB, reset the global list, and empty ListView elements to keep storage and UI in sync.

Highlights

Keys like “name” and “phone” turn user input into structured records that can be retrieved later without separate variables.
Storing list of dictionaries in TinyDB under one tag (“customer data”) simplifies persistence and reload.
update list view builds ListView entries by joining dictionary values (e.g., “Name - Phone”) inside a for-each loop.
Startup loading requires a type-correct fallback: an empty list when the tag doesn’t exist.
Delete all clears both persistent storage and the on-screen ListView so the app can’t show stale records.

Topics

  • Dictionary Blocks
  • Key Value Pairs
  • TinyDB Storage
  • ListView Rendering
  • Customer Records

Mentioned

  • GPT
  • TinyDB