List

A List variable stores an ordered collection of values, internally treated as a JSON array. It's ideal for managing multiple items of the same type, such as product lists, user arrays, menu items, or any sequential data.

When to Use

Use List variables when you need to:

  • Store collections of items (products, users, posts)

  • Manage arrays returned from API calls

  • Populate ListView, GridView, or Carousel widgets

  • Track multiple selections or tags

  • Store ordered data that needs indexing

  • Iterate over a collection of items

Structure

Lists are ordered collections enclosed in square brackets:

["Apple", "Banana", "Orange"]
[1, 2, 3, 4, 5]
[
  {"name": "John", "age": 30},
  {"name": "Jane", "age": 25},
  {"name": "Bob", "age": 35}
]

Common Use Cases

1. Data Source for Lists

Power ListView, GridView, or Carousel widgets:

2. API Response Arrays

Store lists returned from APIs:

3. Multiple Selections

Track selected items:

4. Tags or Categories

Store multiple tags:

5. Simple Arrays

Store simple value lists:

Accessing List Data

By Index

Access items using zero-based index:

Or use the elementAt function:

First and Last Elements

Get the first or last item directly:

Length

Get the number of items:

In Loops (currentItem)

When used as a data source in ListView/GridView:

Modifying Lists

Adding Items

Add items using standard array notation or API responses:

Note: For adding individual items, use JSON operators to create a new array with the added item.

Clearing List

Empty the entire list:

Working with Lists

List Operators

Use list-specific operators for manipulation. Available functions include:

  • contains() - Check if item exists in the list

  • elementAt() - Access element at specific index

  • firstElement() - Get the first element

  • lastElement() - Get the last element

  • skip() - Skip first N elements

  • take() - Take first N elements

  • reversed() - Reverse the order of elements

See the JSON Operators documentation for all available array functions.

Checking for Items

Verify if an item exists:

Taking Subsets

Extract portions of the list:

Reversing

Reverse the order:

Common Patterns

Empty State Handling

Check if list is empty:

Accessing First/Last Item

Checking if Empty

Displaying Count

Showing Limited Items

Pagination Pattern

Iterating in UI

List of Objects vs List of Primitives

List of Objects

Best for complex data with multiple properties:

List of Primitives

Best for simple values:

Best Practices

  • Initialize with empty array: Start with [] if no initial data, to avoid null errors

  • Use for homogeneous data: Lists work best when all items have the same structure

  • Check length before accessing: Always verify myList.length > 0 before accessing items

  • Handle empty states: Provide UI feedback when lists are empty

  • Use appropriate data structures: Use List for arrays, JSON for single objects

  • Keep items consistent: If storing objects, ensure all have the same properties

  • Index safety: Check bounds before accessing by index to avoid errors

  • Immutable updates: When modifying, create new arrays rather than mutating directly

Performance Considerations

  • Lazy rendering: Use ListView/GridView for long lists—they render items on-demand

  • Avoid large static lists: For very large datasets, fetch data in pages/chunks

  • Filter efficiently: Filter lists before passing to UI widgets when possible

  • Minimize re-renders: Only update the list when necessary to avoid performance issues

  • JSON Operators - Array manipulation functions

  • ListView - Display lists in UI

  • GridView - Display grids in UI

  • Carousel - Display carousels

  • API Calls - Fetching list data from APIs

  • Variables Overview - Learn about all variable types

Last updated