HandyExtensions
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.
Extensions On:
- BuildContext
- String
- int
- List
- DateTime
- Num
- Map
- Iterable
- Nullable String (probably useless π but it's there)
Getting started
Add as a dependency
dependencies:
handy_extensions: <version>
Usage
BuildContext
Navigate To A Page
context.goTo(page: const Home());
Navigate To A Page Replacing The Current Page
context.goTo(page: const Home(), replace: true);
Go Back To The Previous Page
context.goBack();
Navigate To A Page And Remove History
context.goToRefresh(page: const Login());
Notify The User Using A SnackBar
context.notify(message: 'Hello World', isError: false); // You can pass the isError argument or leave it, it will default to false
Get Screen Size (based on MediaQuery)
// Height
context.height;
// Width
context.width;
String
Country Emoji
String country = 'ZW';
String emoji = country.countryEmoji; // => πΏπΌ (String)
Title Case
String title = 'hello world';
String titleCase = title.titleCase; // => Hello world
Heading Case
String heading = 'hello world';
String headingCase = heading.headingCase; // => Hello World
doubleOrNull
String? number = '1';
double? doubleNumber = number.doubleOrNull; // => 1.0 (double) or null (null)
intOrNull
String? number = '1';
int? intNumber = number.intOrNull; // => 1 (int) or null (null)
Map
swap
Map<String, int> map = {'a': 1, 'b': 2};
map.swap; // {1: 'a', 2: 'b'}
copy
Map<String, int> map = {'a': 1, 'b': 2};
map.copy; // {'a': 1, 'b': 2}
removeNulls
Map<String, int?> map = {'a': 1, 'b': null};
map.removeNulls; // {'a': 1}
adjustOrder
Map<String, int> map = {'a': 1, 'b': 2};
map.adjustOrder(1, 0); // {'b': 2, 'a': 1}
List
Partition into chunks
[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
Random Item
// Will return a random item from the list of type T
["Hello", "World", "iAMNGONI"].randomItem();
Random Items
// 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);
firstWhereOrNull
List<String> list = ['a', 'b', 'c'];
String? character = list.firstWhereOrNull( (String item) => item == 'a'); // => 'a' (String) or null (null)
groupBy
[1,2,3,4,5,6].groupBy((i) => i % 2 == 0); // {true: [2, 4, 6], false: [1, 3, 5]}
swap
List<int> list = [1, 2, 3, 4, 5];
list.swap(0, 4); // [5, 2, 3, 4, 1]
swapRange
List<int> list = [1, 2, 3, 4, 5];
list.swapRange(0, 2, 3); // [4, 5, 3, 1, 2]
hasDuplicates
List<int> list = [1, 2, 3, 4, 5, 1];
list.hasDuplicates; // true
intersperse
List<int> list = [1, 2, 3, 4, 5, 1];
list.intersperse(100);
// [1, 100, 2, 100, 3, 100, 4, 100, 5, 100, 1]
Int
microsecond
Duration microsecond = 1.microsecond; // => 1
milliseconds
Duration milliseconds = 1.milliseconds; // => 1
seconds
Duration seconds = 1.seconds; // => 1
minutes
Duration minutes = 1.minutes; // => 1
hours
Duration hours = 1.hours; // => 1
days
Duration days = 1.days; // => 1
weeks
Duration weeks = 1.weeks; // => 1
Example usage of the above duration items
Duration duration = 1.weeks + 2.days + 3.hours + 4.minutes + 5.seconds + 6.milliseconds + 7.microseconds;
General
Check if variable is null
String? name = null;
name.isNull; // => true (bool)
Nullable Int
isInt
int? number = 1;
number.isInt; // => true (bool)
int? number = null;
number.isInt; // => false (bool)
Nullable String
isString
String? name = 'Ngoni';
name.isString; // => true (bool)
String? name = null;
name.isString; // => false (bool)
orEmpty
String? name = null;
name.orEmpty; // => '' (String)
String? name = 'Ngoni';
name.orEmpty; // => 'Ngoni' (String)
isNotReallyEmpty
String? name = null;
name.isNotReallyEmpty; // => false (bool)
String? name = ' ';
name.isNotReallyEmpty; // => false (bool)
String? name = 'Ngoni';
name.isNotReallyEmpty; // => true (bool)
Additional information
You can add in more extensions of your own -> share with the rest of the world.
Libraries
- handy_extensions
- This library is a collection of handy extensions for Dart's core types.