11import 'package:nullx/nullx.dart' ;
22
3- void main () {
3+ void main () async {
44 /// Variables
55
66 // ignore: unnecessary_nullable_for_final_variable_declarations
@@ -13,6 +13,12 @@ void main() {
1313 const String ? nullString = null ;
1414 const double ? nullDouble = null ;
1515 const bool ? nullBool = null ;
16+ // ignore: unnecessary_nullable_for_final_variable_declarations
17+ final Map <String , int >? nullableMap = {'a' : 1 , 'b' : 2 };
18+ // ignore: unnecessary_nullable_for_final_variable_declarations
19+ final Future <int ?>? nullableFuture = Future .value (42 );
20+ // ignore: unnecessary_nullable_for_final_variable_declarations
21+ final Future <int ?>? failedFuture = Future .error (Exception ('Failed' ));
1622
1723 // ignore: unnecessary_nullable_for_final_variable_declarations
1824 final List <int ?>? nullableIntList = [1 , null , 3 , null ];
@@ -183,6 +189,103 @@ void main() {
183189 // Performs an operation on the age if it's not null
184190 age.let ((a) => a);
185191
192+ // Check if the map is null or empty
193+ // ignore: avoid_print
194+ print (nullableMap.isNullOrEmpty); // false
195+
196+ // Get value for key or return default
197+ // ignore: avoid_print
198+ print (nullableMap.getOrElse ('c' , 0 )); // 0
199+
200+ // Put a value if the key is absent
201+ nullableMap.putIfAbsentOrElse ('c' , 3 );
202+ // ignore: avoid_print
203+ print (nullableMap); // {a: 1, b: 2, c: 3}
204+
205+ // Update a value using a function
206+ nullableMap.updateValue ('a' , (value) => value! + 10 );
207+ // ignore: avoid_print
208+ print (nullableMap); // {a: 11, b: 2, c: 3}
209+ // ignore: avoid_print
210+
211+ // Filter the map
212+ final filteredMap = nullableMap.filter ((entry) => entry.value > 2 );
213+ // ignore: avoid_print
214+ print (filteredMap); // {a: 11, c: 3}
215+
216+ // Map keys and values
217+ final mappedMap = nullableMap.mapKeysAndValues (
218+ (entry) => MapEntry (entry.key.toUpperCase (), entry.value.toString ()),
219+ );
220+ // ignore: avoid_print
221+ print (mappedMap); // {A: 11, B: 2, C: 3}
222+
223+ // Iterate through the map
224+ // ignore: avoid_print
225+ nullableMap.forEachEntry ((key, value) => print ('$key : $value ' ));
226+ // Output:
227+ // a: 11
228+ // b: 2
229+ // c: 3
230+
231+ // Check if the map contains a key or value
232+ // ignore: avoid_print
233+ print (nullableMap.containsKeyOrNull ('a' )); // true
234+ // ignore: avoid_print
235+ print (nullableMap.containsValueOrNull (4 )); // false
236+
237+ // Return a default value if the Future completes with null
238+ final int result2 = await nullableFuture.orDefault (5 );
239+ // ignore: avoid_print
240+ print (result2); // 42
241+
242+ // Return null if the Future completes with an error
243+ final int ? errorHandled = await failedFuture.onErrorReturnNull ();
244+ // ignore: avoid_print
245+ print (errorHandled); // null
246+
247+ // Return a default value if the Future completes with an error
248+ final int ? errorHandledWithValue = await failedFuture.onErrorReturn (5 );
249+ // ignore: avoid_print
250+ print (errorHandledWithValue); // 5
251+
252+ // Provide an alternative Future if the original completes with null
253+ // ignore: unnecessary_nullable_for_final_variable_declarations
254+ final Future <int ?>? nullableFutureWithNull = Future .value ();
255+ final int alternative = await nullableFutureWithNull.orElse (() async => 99 );
256+ // ignore: avoid_print
257+ print (alternative); // 99
258+
259+ // Execute an action when the Future completes
260+ // ignore: avoid_print
261+ await nullableFuture.whenComplete (() => print ('Completed' )); // Completed
262+
263+ // Ignore any errors the Future may throw
264+ await failedFuture.ignoreErrors (); // No output, error ignored
265+
266+ // Timeout a Future and return null if it doesn't complete in time
267+ // ignore: unnecessary_nullable_for_final_variable_declarations
268+ final Future <int ?>? slowFuture =
269+ Future .delayed (const Duration (seconds: 2 ), () => 10 );
270+ final int ? timedOut =
271+ await slowFuture.timeoutWithNull (const Duration (seconds: 1 ));
272+ // ignore: avoid_print
273+ print (timedOut); // null
274+
275+ // Chain another Future using thenOrNull
276+ final Future <String ?> chained =
277+ nullableFuture.thenOrNull ((value) => Future .value ('Value: $value ' ));
278+ // ignore: avoid_print
279+ print (await chained); // Value: 42
280+
281+ // Catch an error and return null using catchErrorOrNull
282+ final int ? caughtError = await failedFuture.catchErrorOrNull ((error) {
283+ // ignore: avoid_print
284+ print ('Caught error: $error ' );
285+ });
286+ // ignore: avoid_print
287+ print (caughtError); // Caught error: Exception: Failed, null
288+
186289 // Throws a [NotImplementedError] indicating that an operation is
187290 try {
188291 todo ();
0 commit comments