v1.0.0 - Initial release (#1)
CI and Release / format (push) Successful in 34s
CI and Release / analyze (push) Successful in 26s
CI and Release / test (push) Successful in 48s
CI and Release / pana (push) Successful in 2m9s
CI and Release / version-and-changelog (push) Successful in 20s
CI and Release / publish (push) Failing after 13s
CI and Release / format (push) Successful in 34s
CI and Release / analyze (push) Successful in 26s
CI and Release / test (push) Successful in 48s
CI and Release / pana (push) Successful in 2m9s
CI and Release / version-and-changelog (push) Successful in 20s
CI and Release / publish (push) Failing after 13s
Initial release 🥳 Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import 'package:list_or/list_or.dart';
|
||||
|
||||
void main() {
|
||||
print('--- Example 1: Wrapping a Single Value ---');
|
||||
// A single value is converted into a list containing just that value.
|
||||
final singleItem = ListOr<String>('Hello World');
|
||||
|
||||
print('Length: ${singleItem.length}'); // Output: 1
|
||||
print('First item: ${singleItem[0]}'); // Output: Hello World
|
||||
|
||||
// Notice the custom toString():it prints just the string, not ['Hello World']
|
||||
print('String representation: $singleItem'); // Output: Hello World
|
||||
|
||||
print('\n--- Example 2: Wrapping an Iterable ---');
|
||||
// A standard List or Set is normalized into a modifiable ListOr.
|
||||
final multipleItems = ListOr<String>(['Apple', 'Banana']);
|
||||
|
||||
// Because it mixes in ListMixin, you can use all standard list methods.
|
||||
multipleItems.add('Cherry');
|
||||
|
||||
print('Length: ${multipleItems.length}'); // Output: 3
|
||||
|
||||
// When the length is not exactly 1, toString() acts like a normal List.
|
||||
print(
|
||||
'String representation: $multipleItems',
|
||||
); // Output: [Apple, Banana, Cherry]
|
||||
|
||||
print('\n--- Example 3: Parsing Flexible JSON Data ---');
|
||||
// Imagine an API that irregularly returns 'tags' as either a String or a List.
|
||||
final Map<String, dynamic> jsonResponse1 = {'id': 1, 'tags': 'flutter'};
|
||||
final Map<String, dynamic> jsonResponse2 = {
|
||||
'id': 2,
|
||||
'tags': ['dart', 'backend'],
|
||||
};
|
||||
|
||||
// ListOr cleanly normalizes both scenarios without complex type checking.
|
||||
final tags1 = ListOr<String>(jsonResponse1['tags']);
|
||||
final tags2 = ListOr<String>(jsonResponse2['tags']);
|
||||
|
||||
print(
|
||||
'Tags 1 contains "flutter": ${tags1.contains('flutter')}',
|
||||
); // Output: true
|
||||
print('Tags 2 length: ${tags2.length}'); // Output: 2
|
||||
|
||||
print('\n--- Example 4: Handling Nulls with Nullable Types ---');
|
||||
// If your data might explicitly be null and you want to keep that null
|
||||
// as a valid single element, you must use a nullable generic type.
|
||||
final nullableList = ListOr<int?>(null);
|
||||
|
||||
print('Nullable list length: ${nullableList.length}'); // Output: 1
|
||||
print('First item: ${nullableList.first}'); // Output: null
|
||||
|
||||
// Note: Trying to do the following would throw an ArgumentError,
|
||||
// keeping your types safe:
|
||||
// final strictList = ListOr<int>(null); // THROWS!
|
||||
}
|
||||
Reference in New Issue
Block a user