ModelLessArray<T> class
The ModelLessArray class is a generic class that provides a structure for managing a collection of items, specifically designed to work with ModelLess objects. This class offers features such as accessing items by index, dynamically setting and getting values, parsing JSON into the array, and retrieving nested data using paths. Below is an explanation and breakdown of the key features:
Constructors
-
ModelLessArray({List<
T> ? fields})
Properties
Methods
-
addAll(
ModelLessArray< T> value) → void -
Adds all elements from another ModelLessArray to this array.
If the fields list is null, it initializes it before adding the elements.
valueThe ModelLessArray whose elements are to be added to this array. -
clear(
) → void - Clears all elements from the array. If the fields list is null, it initializes it before clearing. After calling this method, the array will be empty.
-
forEach<
R> (T function(R val)) → void -
Iterates over each element in the array and applies the provided function.
The function takes an element of type R as input and returns a value of type T.
functionA function that takes an element of type R and returns a value of type T. Example: -
get<
T> (int index, {dynamic def}) → T -
getByPath<
R> (List path, {dynamic def}) → R -
Retrieves a value of type
Rfrom the array using a specified path. The path is a list of keys and/or indices that navigate through nested ModelLess or ModelLessArray structures. If the path cannot be fully resolved, the method returns the provided default value.pathA list of keys and/or indices representing the navigation path.defA default value of typeRthat is returned if the path -
getByPathString<
R> (String path, {dynamic def}) → R -
Retrieves a value of type
Rfrom the array using a string path. The path is a '/'-separated string that navigates through nested ModelLess or ModelLessArray structures. If the path cannot be fully resolved, the method returns the provided default value.pathA '/'-separated string representing the navigation path.defA default value of typeRthat is returned if the path -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
set(
dynamic value) → void -
Adds a value to the end of the array.
If the fields list is null, it initializes it before adding the value.
valueThe value to add to the array. -
toString(
) → String -
A string representation of this object.
override
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
-
operator [](
int index) → dynamic -
operator []=(
int index, T value) → void -
Sets the value at the specified index in the array.
If the index is out of range, an exception is thrown.
indexThe index at which to set the value.valueThe value to set at the specified index. Example:
Static Methods
-
fromJson<
T> (String jsonString) → ModelLessArray< T> -
Creates a ModelLessArray from a JSON string.
The JSON string should represent a list of objects.
Each object in the list is converted to a ModelLess instance and added to the array.
If parsing fails, an error message is logged to the console.
jsonStringThe JSON string to parse. Returns a ModelLessArray containing the parsed ModelLess instances. -
fromList(
List list) → ModelLessArray -
Creates a ModelLessArray from a List.
Each element in the list is added to the ModelLessArray.
If an element is a Map, it is converted to a ModelLess instance before being added.
listThe List to convert into a ModelLessArray. Returns a ModelLessArray containing the elements from the list. -
tryJson<
T> (String jsonString) → ModelLessArray< T> ? -
Attempts to create a ModelLessArray from a JSON string.
The JSON string should represent a list of objects.
Each object in the list is converted to a ModelLess instance and added to the array.
If parsing fails, the method returns null.
jsonStringThe JSON string to parse. Returns a ModelLessArray containing the parsed ModelLess instances, or null if parsing fails