Developed with 💙 by Ngonidzashe Mangudya
Handy Extension is just a simple library with extensions to the core libraries to make them more handy and quicker to use.
I don't know how "deadly" this is but I just use it anyway.
- BuildContext
- String
- int
- List
- DateTime
- Num
- Map
- Iterable
- Nullable String (probably useless 😂 but it's there)
dependencies:
handy_extensions: <version>
context.goTo(page: const Home());
context.goTo(page: const Home(), replace: true);
context.goBack();
context.goToRefresh(page: const Login());
context.notify(message: 'Hello World', isError: false); // You can pass the isError argument or leave it, it will default to false
// Height
context.height;
// Width
context.width;
String country = 'ZW';
String emoji = country.countryEmoji; // => 🇿🇼 (String)
String title = 'hello world';
String titleCase = title.titleCase; // => Hello world
String heading = 'hello world';
String headingCase = heading.headingCase; // => Hello World
String? number = '1';
double? doubleNumber = number.doubleOrNull; // => 1.0 (double) or null (null)
String? number = '1';
int? intNumber = number.intOrNull; // => 1 (int) or null (null)
Map<String, int> map = {'a': 1, 'b': 2};
map.swap; // {1: 'a', 2: 'b'}
Map<String, int> map = {'a': 1, 'b': 2};
map.copy; // {'a': 1, 'b': 2}
Map<String, int?> map = {'a': 1, 'b': null};
map.removeNulls; // {'a': 1}
Map<String, int> map = {'a': 1, 'b': 2};
map.adjustOrder(1, 0); // {'b': 2, 'a': 1}
[1,2,3,4,5,6].partition(chunkSize: 3); // => [[1,2,3],[4,5,6]] (List<List<int>>). By default it will partition into chunks of 2
// Will return a random item from the list of type T
["Hello", "World", "iAMNGONI"].randomItem();
// Will return a list of random items from the list of type T. By default this may return a
// list with only one item
["Hello", "World", "iAMNGONI"].randomItems(count: 2);
List<String> list = ['a', 'b', 'c'];
String? character = list.firstWhereOrNull( (String item) => item == 'a'); // => 'a' (String) or null (null)
[1,2,3,4,5,6].groupBy((i) => i % 2 == 0); // {true: [2, 4, 6], false: [1, 3, 5]}
List<int> list = [1, 2, 3, 4, 5];
list.swap(0, 4); // [5, 2, 3, 4, 1]
List<int> list = [1, 2, 3, 4, 5];
list.swapRange(0, 2, 3); // [4, 5, 3, 1, 2]
List<int> list = [1, 2, 3, 4, 5, 1];
list.hasDuplicates; // true
List<int> list = [1, 2, 3, 4, 5, 1];
list.intersperse(100);
// [1, 100, 2, 100, 3, 100, 4, 100, 5, 100, 1]
List<int> numbers = [1, 2, 3, 4, 5];
numbers.updateWhere(
(n) => n.isEven, // predicate
(n) => n * 2, // update function
);
// [1, 4, 3, 8, 5]
// Works with any type
List<String> words = ['hello', 'world', 'dart'];
words.updateWhere(
(s) => s.length <= 4,
(s) => s.toUpperCase(),
);
// ['HELLO', 'world', 'DART']
Duration microsecond = 1.microsecond; // => 1
Duration milliseconds = 1.milliseconds; // => 1
Duration seconds = 1.seconds; // => 1
Duration minutes = 1.minutes; // => 1
Duration hours = 1.hours; // => 1
Duration days = 1.days; // => 1
Duration weeks = 1.weeks; // => 1
Duration duration = 1.weeks + 2.days + 3.hours + 4.minutes + 5.seconds + 6.milliseconds + 7.microseconds;
String? name = null;
name.isNull; // => true (bool)
int? number = 1;
number.isInt; // => true (bool)
int? number = null;
number.isInt; // => false (bool)
String? name = 'Ngoni';
name.isString; // => true (bool)
String? name = null;
name.isString; // => false (bool)
String? name = null;
name.orEmpty; // => '' (String)
String? name = 'Ngoni';
name.orEmpty; // => 'Ngoni' (String)
String? name = null;
name.isNotReallyEmpty; // => false (bool)
String? name = ' ';
name.isNotReallyEmpty; // => false (bool)
String? name = 'Ngoni';
name.isNotReallyEmpty; // => true (bool)
You can add in more extensions of your own -> share with the rest of the world.