extension_utils
![pub package]()
A modern Dart/Flutter utility library providing practical extensions for String, List, Map, num, DateTime, Duration, Color, Iterable, and Enum types.
Requires Dart ≥ 3.3.0 and Flutter ≥ 3.19.0.
Features
| Extension |
Description |
StringUtils |
Case conversion, validation, formatting, encoding |
ListUtils |
Statistics, sorting, windowing, zipping, sampling |
MapUtils |
Deep access, merging, key/value mapping, query strings |
NumberUtils |
Math, formatting, numeral systems, ordinals |
DateTimeUtils |
Boundaries, relative time, formatting, workday arithmetic |
DurationUtils |
Human-readable formatting, ago/fromNow helpers |
ColorUtils |
Lighten/darken, palettes, contrast, hex conversion |
IterableUtils |
Safe access, aggregation, grouping, chunking |
EnumUtils |
Pattern matching, human-readable labels |
Installation
dependencies:
extension_utils: ^2.0.0
import 'package:extension_utils/extension_utils.dart';
API Reference
String Extensions
'hello world'.toCamelCase()
'hello world'.toPascalCase()
'hello world'.toSnakeCase()
'hello world'.toKebabCase()
'hello world'.toTitleCase()
'hello world'.toConstantCase()
'hello world'.capitalize()
'user@example.com'.isEmail()
'https://dart.dev'.isUrl()
'+1-800-555-0100'.isPhoneNumber()
'#FF5733'.isHexColor()
'dad'.isPalindrome()
'12345'.isDigits()
'hello'.isAlpha()
'hello world'.toSlug()
'John Doe'.initials()
'Hello World'.reverse()
'1234567890'.mask(visibleCount: 4)
'Long text here'.truncate(8)
'<b>bold</b>'.stripHtml()
'hello'.toBase64()
'aGVsbG8='.fromBase64()
'hello world'.wordCount
'hello'.countOccurrences('l')
'hello'.equalsIgnoreCase('HELLO')
'hello {name}!'.formatMap({'name': 'world'})
'hello {}!'.format(['world'])
'hello'.repeat(3, separator: '-')
'hello world'.between('hello ', '!')
'hello world'.before(' ')
'hello world'.after(' ')
'hello world'.dropLeft(6)
'hello world'.dropRight(6)
'hello world'.removePrefix('hello ')
'hello world'.removeSuffix(' world')
List Extensions
[1, 2, 3].second
[1, 2, 3].third
[1, 2, 3].penultimate
[1, 2, 1, 3].removeFirst(1)
[1, 2, 1, 3].removeLastOccurrence(1)
[1, 2, 1, 3].removeAll(1)
['b', 'a', 'c'].sortedBy((s) => s)
['b', 'a', 'c'].sortedByDescending((s) => s)
[[1, 2], [3, 4]].flatten()
[1, 2, 3].zip(['a', 'b', 'c'])
[1, 2, 3, 4, 5].windowed(3)
[1, 2, 3].rotate(1)
[1, 2, 3, 4].average
[1, 2, 3, 4].median
[1, 1, 2, 3].mode
[1, 2, 3].sumBy((n) => n * 2)
[1, 2, 3, 4, 5].random()
[1, 2, 3, 4, 5].sample(3)
[1, 2, 3, 4].groupBy((n) => n.isEven ? 'even' : 'odd')
[1, 2, 3].joinToString(', ')
Map Extensions
{'a': 1}.getOrDefault('b', 0)
{'a': 1}.getOrPut('b', () => 2)
{'a': 1, 'b': 2}.mapKeys((k) => k.toUpperCase())
{'a': 1, 'b': 2}.mapValues((v) => v * 10)
{'a': 1, 'b': 2}.invertMap()
{'a': 1}.mergeWith({'b': 2})
{'a': 1, 'b': 2}.filter((k, v) => v > 1)
{'a': 1, 'b': 2}.filterKeys(['a'])
{'a': 1, 'b': 2}.filterValues([2])
{'a': {'b': {'c': 42}}}.deepGet('a.b.c')
{'q': 'dart', 'page': '1'}.toQueryString()
{'a': {'b': 1}}.flatten()
Number Extensions
0.0.lerp(10.0, 0.5)
5.0.normalize(0, 10)
5.factorial()
7.isPrime
3.14159.toRadians()
1.5708.toDegrees()
1234567.toCurrencyString()
1234567.toCurrencyString(symbol: '€')
3.toOrdinal()
2024.toRoman()
255.toBinary()
255.toHex()
255.toOctal()
3.14159.roundTo(2)
3.14159.toPrecision(4)
42.pad(5)
25.0.percentage(200)
1234.digitCount
DateTime Extensions
DateTime.now().startOfDay
DateTime.now().endOfDay
DateTime.now().startOfWeek
DateTime.now().startOfMonth
DateTime.now().startOfYear
DateTime.now().subtract(Duration(hours: 2)).timeAgo()
DateTime.now().add(Duration(days: 3)).timeAgo()
DateTime(2024, 3, 5).format('dd MMM yyyy')
DateTime(2024, 3, 5).format('yyyy-MM-dd')
DateTime(2024, 3, 5).format('MMMM')
DateTime(2024, 3, 5).format('EEE')
DateTime.now().isToday
DateTime.now().isWeekend
DateTime.now().isLeapYear
DateTime(2024, 3, 5).isBetween(DateTime(2024, 1, 1), DateTime(2024, 12, 31))
DateTime.now().addWorkdays(5)
DateTime(2000, 6, 15).age
DateTime.now().quarterOfYear
DateTime.now().weekOfYear
DateTime.now().season
DateTime.now().toUtcIso8601
Duration Extensions
const Duration(hours: 1, minutes: 23, seconds: 45).formatted
const Duration(minutes: 90).toHhMmSs()
const Duration(seconds: 75).toMmSs()
const Duration(hours: 2).ago
const Duration(days: 3).fromNow
const Duration(days: 14).inWeeks
const Duration.zero.isZero
Color Extensions
Colors.blue.isLight
Colors.blue.isDark
Colors.blue.luminance
Colors.blue.contrastColor
Colors.blue.lighten(0.2)
Colors.blue.darken(0.2)
Colors.blue.grayscale
Colors.red.mix(Colors.blue, 0.5)
Colors.red.complementary
Colors.blue.analogous(count: 3)
Colors.blue.triadic
Colors.blue.toHex()
Colors.blue.toMaterialColor()
Iterable Extensions
[].firstOrNull
[1].singleOrNull
[1, 2].singleOrNull
[1, 2, 3, 4].count((n) => n.isEven)
[1, 3, 5].none((n) => n.isEven)
[1, 2, 3].sumBy((n) => n * 2)
[1, 2, 3].averageBy((n) => n)
['a', 'bb', 'ccc'].maxBy((s) => s.length)
['a', 'bb', 'ccc'].minBy((s) => s.length)
[1, 2, 3].forEachIndexed((i, e) => print('$i: $e'))
[1, 2, 3].mapIndexed((i, e) => '$i:$e').toList()
['hello', 'world'].flatMap((s) => s.split('')).toList()
[1, 2, 3, 4, 5].chunked(2).toList()
users.groupBy((u) => u.role)
users.associateBy((u) => u.id)
Enum Extensions
enum Status { active, inactive, pending }
Status.active.label
Status.active.when({
Status.active: () => 'Running',
Status.inactive: () => 'Stopped',
Status.pending: () => 'Waiting',
});
Status.active.whenOrElse({
Status.inactive: () => 'Stopped',
}, orElse: () => 'Other');
Contributing
Issues and pull requests are welcome at github.com/oi-narendra/extension_utils.
License
MIT — see LICENSE for details.