Choice<T> class

Choice A generic class that standardizes elements for consistent manipulation.

The Choice class provides a standardized way to handle different types of elements within a list, menu, or selection field. Each instance of Choice contains a unique key (key), a display value (value), and optional metadata of any type, making it highly versatile.

This class is particularly useful in scenarios where selections, dropdowns, or other user interface components require a consistent structure for the data presented to the user.

Use of the key Field:

The key field serves a dual purpose:

  1. Quick Validations:

    • Unique Identification: Each Choice can have a unique key that facilitates the identification and validation of selected items.
    • Search and Filter Operations: Enables efficient searches and filters based on the key.
  2. Grouping of Elements:

    • Contextual Grouping: By assigning a specific key (e.g., an empty string '' or null ), it can be used to group the following elements under a unique context.
    • Group Title: If a Choice has an empty key ('' or null), it is interpreted as the title of a group. All elements following this title, until another Choice with an empty key is encountered, will be grouped under this title.
    • That null value is recommended

Grouping Example:

List<Choice<dynamic>> options = [
  Choice('Group A'), // Group title
  Choice('1', 'Option A1'),
  Choice('2', 'Option A2'),
  Choice('', 'Group B'), // New group title
  Choice('3', 'Option B1'),
  Choice('4', 'Option B2'),
];

In this example:

  • "Group A" is the title of the first group containing "Option A1" and "Option A2".
  • "Group B" is the title of the second group containing "Option B1" and "Option B2".

Parameters:

  • key (String or null):
    • Description: An optional and unique identifier for the Choice.
    • Usage in Validations: Facilitates quick validations and search operations.
    • Usage in Grouping: If left empty (''), the Choice will act as the title of a group. The following Choices will be part of this group until another empty key is found.
    • Note: The key cannot be null; it must have a value or be an empty string.
  • value (String):
    • Description: The text to be displayed in the user interface.
  • metadata (T?):
    • Description: Additional data associated with the Choice, which can be used to store extra information related to this item.

Usage Examples:

// A Choice with a string as metadata.
Choice<String> stringChoice = Choice('1', 'Option 1', metadata: 'This is a string');

// A Choice with a UserProfile object as metadata.
Choice<UserProfile> userProfileChoice = Choice('2', 'Jhonatan', metadata: UserProfile('Jhonatan', 30));

// A Choice without metadata, used as a group title.
Choice<void> groupTitle = Choice('User Group');

Complete Class:

class Choice<T> {
  final String key;
  final String value;
  final T? metadata;

  Choice(this.key, this.value, {this.metadata});
}

Constructors

Choice(String? key, String value, {T? metadata})

Properties

hashCode int
The hash code for this object.
no setteroverride
key String?
final
metadata → T?
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
value String
final

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
override