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:
-
Quick Validations:
- Unique Identification: Each
Choice
can have a uniquekey
that facilitates the identification and validation of selected items. - Search and Filter Operations: Enables efficient searches and filters based on the key.
- Unique Identification: Each
-
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 emptykey
(''
ornull
), it is interpreted as the title of a group. All elements following this title, until anotherChoice
with an emptykey
is encountered, will be grouped under this title. - That
null
value is recommended
- Contextual Grouping: By assigning a specific
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
ornull
):- 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 emptykey
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});
}
Properties
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