dart_extensions 1.0.1

Flutter Android iOS web

Set of method-extensions for dart that makes using framework in a much easier and clean way also adds additional functionality.

What New #

  • BuildContext extensions πŸ’ͺ🏻
  • List<Widget> extensions πŸ’ͺ🏻
  • Text extensions πŸ’ͺ🏻
  • Icon extensions πŸ’ͺ🏻
  • .sortBy [0.3.5] Sorts elements in the array in-place according to natural sort order of the value returned by specified selector function.
  • .withTooltip Tooltips improve the accessibility of visual widgets by proving a textual representation of the widget

Why Method Extensions? When you’re using someone else’s API or when you implement a library that’s widely used, it’s often impractical or impossible to change the API. But you might still want to add some functionality.

let me know if you want something specific or you found a bug at bar.idan@gmail.com

Let get started πŸ’ͺ🏻 #

  1. Go to pubspec.yaml
  2. add a dart_extensions and replace [version] with the latest version:
dependencies:  
 dart_extensions: ^[version]
  1. click the packages get button or flutter pub get

Iterable Extensions #

.any() #

Returns true if at least one element matches the given predicate.

final users = [User(22, "Kasey"), User(23, "Jadn")]; 
users.any((u) => u.name == 'Kasey') // true

.groupBy() #

Groups the elements in values by the value returned by key.

final users = [User(22, "Kasey"), User(23, "Jadn"), User(22, "Rene"), User(32, "Aden")]; 

users.groupBy((u) => u.age); 

Sort the users by age:

{  
  22: [User:22, Kasey, User:22, Rene], 
  23: [User:23, Jadn], 
  32: [User:32, Aden]
}

.sortBy() #

Sorts elements in the array in-place according to natural sort order of the value returned by specified selector function.

final users = [User(22, "Kasey"), User(16, "Roni"), User(23, "Jadn")]; 
users.sortBy((u) => u.age) ///  [User(16, "Roni"), [User(22, "Kasey"), User(23, "Jadn")]

.find() #

Returns the first element matching the given predicate, or null if element wasn't found.

final users = [User(22, "Kasey"), User(23, "Jadn"), User(22, "Rene"), User(32, "Aden")]; 

users.find((u) => u.name == "Rene") // User(22, "Rene")

.chunks() #

Splits the Iterable into chunks of the specified size

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].chunks(3)) 

result

([1, 2, 3], [4, 5, 6], [7, 8, 9], [10])

.filter() #

Returns a list containing only elements matching the given predicate, the return type will be List, unlike the where operator that return Iterator, also it filters null.

final users = [User(22, "Kasey"), User(23, "Jadn"), User(22, "Rene"), User(32, "Aden")]; 
final filtered = users.filter((u) => u.name == "Kasey"); // [User(22, "Kasey")] <- Type List<User>

final listWithNull = [null, User(23, "Jadn"), User(22, "Rene"), User(32, "Aden")];
final filtered = listWithNull.filter((u) => u.name == "Jadn"); // [User(23, "Jadn")]

.intersect() #

Returns a set containing all elements that are contained by both this set and the specified collection.

Set.from([1, 2, 3, 4]).intersect(Set.from([3, 4, 5, 6]) // 1,2,3,4,5,6

.filterNot() #

Returns a list containing only not the elements matching the given predicate, the return type will be List, unlike the where operator that return Iterator, also it filters null.

final users = [User(22, "Kasey"), User(23, "Jadn"), User(22, "Rene"), User(32, "Aden")]; 
final filtered = users.filterNot((u) => u.name == "Kasey"); // [User(23, "Jadn"), User(22, "Rene"), User(32, "Aden")] <- Type List<User>

final listWithNull = [null, User(23, "Jadn"), User(22, "Rene"), User(32, "Aden")];
final filtered = listWithNull.filterNot((u) => u.name == "Jadn"); // [User(22, "Rene"), User(32, "Aden")]

.takeOnly() #

Returns a list containing first [n] elements.

[1, 2, 3, 4].takeOnly(1) // [1]

.drop() #

Returns a list containing all elements except first [n] elements.

[1, 2, 3, 4].drop(1) // [2, 3, 4]

.forEachIndexed() #

Performs the given action on each element on iterable, providing sequential index with the element.

["red","green","blue"].forEachIndexed((item, index) { 
	print("$item, $index"); 
}); // 0: red // 1: green // 2: blue```  

.sortedDescending() #

Returns a new list with all elements sorted according to descending natural sort order.

var list = [1,2,3,4,5];  
final descendingList = list.sortedDescending();  
print(descendingList); // [5, 4, 3, 2, 1]

.count() #

Return a number of the existing elements by a specific predicate

final users = [User(33, "Miki"), User(45, "Anna"), User(19, "Amit")];  
  
final aboveAgeTwenty = users.count((user) => user.age > 20);  
print(aboveAgeTwenty); // 2

.associate() #

Creates a Map instance in which the keys and values are computed from the iterable.

final users = [User(33, "Miki"), User(45, "Anna"), User(19, "Amit")];  

users.associate((k) => k.name, (e) => e.age) // 'Miki': 33, 'Anna': 45, 'Amit': 19}

.concatWithMultipleList() #

Return a list concatenates the output of the current list and multiple iterables.

  final listOfLists = [
        [5, 6, 7],
        [8, 9, 10]
      ];
  [1, 2, 3, 4].concatWithMultipleList(listOfLists); 
  
  // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

.distinctBy() #

Returns a list containing only the elements from given collection having distinct keys.

// example 1
final users = ["Zack", "Ian", "Ronit"];  
users.distinctBy((u) => u.toLowerCase().startsWith("z")); // Zack 

// example 2
final users = [User(11, 'idan'), User(12, 'ronit'), User(11, 'asaf')];
	
final dist = users.distinctBy((u) => u.age);    
dist.forEach((u) => print(u.age)); //  11, 12

.zip() #

Zip is used to combine multiple iterables into a single list that contains the combination of them two.


final soldThisMonth = [Motorcycle(2020, 'BMW R1200GS'), Motorcycle(1967, 'Honda GoldWing')];
final soldLastMonth = [Motorcycle(2014, 'Honda Transalp'), Motorcycle(2019, 'Ducati Multistrada')];    
  
final sales = soldThisMonth.zip(soldLastMonth).toList();  
  				
print(sales); // [
  [brand: BMW R1200GS year: 2020, brand: Honda Transalp year: 2014], // first pair from this month and last
  [brand: Honda GoldWing year: 1967, brand: Ducati Multistrada year: 2019] // second pair from last month 
]

See iterable.dart for more examples.

Flutter Extensions #

Context extensions #

Are you not tired from typing MediaQuery.of(context).size... to get height or width? here's a cool extension

  context.mq  // returns the MediaQuery
  context isLandscape // returns if Orientation is landscape
context.sizePx // returns same as MediaQuery.of(context).size
context.widthPx // returns same as MediaQuery.of(context).size.width
context.heightPx // returns same as MediaQuery.of(context).height

Text Extensions #

final text = Text('hello')
     .bold()
     .fontSize(25)
     .italic();

List

    final someWidgetList = [
      Text('hello'),
      Text('world'),
    ].toColumnWidget();  // toRowWidget(), toStackWidget()

Widget extensions #

So now we can just add round corners, shadows, align, and added gestures to our Widgets without the crazy water-fall effect. awesome! That's just the tip of the iceberg, expect to see very cool stuff soon.

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: Stack(
          children: <Widget>[
            Container(
              height: 100,
              width: 100,
            )   .withRoundCorners(backgroundColor: Colors.grey)
                .withShadow()
                .alignAtCenter()
                .toCenter()
                .withTooltip('just a tooltip')
                .paddingOnly(left: 10)
                .paddingAll(20)
                .onTap(() => print('tap'))
                .onLongPress(() => print('long press'))
          ],
        ),
      ),
    );
  }
}

We can navigate from every widget by calling these methods

    navigateTo(route: MaterialPageRoute(builder: (c) => Login()));
    navigateByRouteName(Routes.home, );
    final result = navigateBack();

Http Extensions #

.httpGet() #

Sends an HTTP GET request with the given headers to the given URL

final json = await "https://jsonplaceholder.typicode.com/posts".httpGet();

result:

[
  {
    "userId": 1,
    "id": 1,
    "title": "sunt aut facere",
    "body": "quia et suscipit"
  },
  {
    "userId": 1,
    "id": 2,
    "title": "qui est esse",
    "body": "dolor beatae ea dolores neque"
  },
]

usage with then:

"https://jsonplaceholder.typicode.com/posts".httpGet().then((result) {
          print(result);
       }).catchError((e) => print(e));

.httpPost() #

Sends an HTTP POST request with the given headers and body to the given URL which can be a [Uri] or a [String].

String json = '{"title": "Hello", "body": "body text", "userId": 1}';
final json = await "https://jsonplaceholder.typicode.com/posts".httpPost(json);

for more examples (put, delete) See http.dart

Range Extensions #

.until() #

Returns a sequence of integer, starting from the current number until the [end] number. [step] is optional, it will step number if given

for(final num in 1.until(10)) {
  numbers.add(num); 
}

result

[1, 2, 3, 4, 5, 6, 7, 8, 9]

with step:

for(final num in 1.until(10, step: 2)) {
  numbers.add(num); 
}

result

[1, 3, 5, 7, 9]

String Extensions #

.asBool() #

Returns true if this string is any of these values: "true", "yes", "1", or if the string is a number and greater than 0, false if less than 1. This is also case insensitive.

'true'.asBool  // true
'True'.asBool  // true
'false'.asBool //  false
'False'.asBool //  false
'yes'.asBool   // true
'YES'.asBool   // true
'no'.asBool    // false
'NO'.asBool    // false

.insert() #

Returns a new string in which a specified string is inserted at a specified index position in this instance.

'test'.insert(1, 't') // 'ttest'
'123456890'.insert(6, '7') // '1234567890'
'dart cool'.insert(4, ' is') // 'dart is cool'

.isNullOrWhiteSpace() #

Indicates whether a specified string is null, empty, or consists only of white-space characters.

'test'.isNullOrWhiteSpace // false
'   '.isNullOrWhiteSpace, // true
null.isNullOrWhiteSpace, // true
'  te  st  '.isNullOrWhiteSpace // false

.replaceAfter() #

Replace part of string after the first occurrence of given delimiter.

print("myemail@".replaceAfter("@", "gmail.com")); // myemail@gmail.com 

.replaceBefore() #

Replace part of string before the first occurrence of given delimiter.

print('@domain.com'.replaceBefore('@', "name")); // "name@domain.com"

.anyChar() #

Returns true if at least one element matches the given predicate

'test'.anyChar((c) => c == 't'); // true;
'test'.anyChar((c) => c == 'd'); // false;

.ifEmpty() #

If the string is empty perform an action.

"".ifEmpty(() => print("do any action here")); // do any action here

.toDoubleOrNull() #

Parses the string as an integer or returns null if it is not a number.

var number = '12345'.toDoubleOrNull(); // 12345  
var notANumber = '123-45'.toDoubleOrNull(); // null  

.limitFromEnd() #

Limit the string to a maximum length, taking from the end of the string.

var longString = "0123456789";
var noMoreThanThree = longString.limitFromEnd(3); // "789"

.limitFromStart() #

Limit the string to a maximum length, taking from the start of the string.

var longString = "0123456789";
var noMoreThanThree = longString.limitFromStart(3); // "012"

int Extensions #

.inRangeOf() #

Return the min if this number is smaller then minimum Return the max if this number is bigger the the maximum Return this number if it's between the range

1.inRangeOf(0, 3) // 1 number in range so will return the number
2.inRangeOf(3, 4) // 3 number is smaller then the range so will return min
5.inRangeOf(3, 4) // 4 number is bigger then the range so will return max

Flutter Extensions Full List #

Flutter #

  • Tooltip
  • algin
  • center
  • container
  • padding
  • navigation
  • Context
  • Text
  • List<Widget>
  • Icon

Http Extensions #

  • httpGet
  • httpPost
  • httpPut
  • httpDelete

Iterables Extensions #

  • sortBy
  • toMutableSet
  • intersect
  • groupBy
  • find
  • filter
  • filterNot
  • isEmptyOrNull
  • chunks
  • zip
  • half
  • takeOnly
  • drop
  • firstHalf
  • secondHalf
  • swap
  • getRandom
  • firstOrNull
  • firstOrNullWhere
  • firstOrDefault
  • lastOrNull
  • lastOrDefault
  • forEachIndexed
  • sortedDescending
  • containsAll
  • count
  • distinctBy
  • subtract
  • concatWithSingleList
  • concatWithMultipleList
  • associate

Range Extensions #

  • until

Strings Extensions #

  • validateEmail
  • removeSurrounding
  • isNullOrEmpty
  • replaceAfter
  • replaceBefore
  • orEmpty
  • ifEmpty
  • lastIndex
  • printThis
  • equalsIgnoreCase
  • toDoubleOrNull
  • toIntOrNull
  • anyChar
  • removeAllWhiteSpace
  • isNotBlank
  • toCharArray
  • insert
  • isNullOrWhiteSpace
  • asBool

DateTime Extensions #

  • toMilliseconds
  • toSeconds
  • toMinutes
  • toHours
  • toDays
  • isToday
  • addOrRemoveYears
  • addOrRemoveMonth
  • addOrRemoveDay
  • addOrRemoveMinutes
  • addOrRemoveSeconds
  • startOfDay
  • startOfMonth
  • startOfYear
  • operator to add dates
  • operator to subtract dates
  • tomorrow
  • yesterday
  • min
  • max
  • isLeapYear
  • limitFromEnd
  • limitFromStart

Integers Extensions #

  • inRangeOf
  • absolute
  • isEven
  • isOdd
  • isPositive
  • isNegative
  • tenth
  • fourth
  • third
  • half
  • doubled
  • tripled
  • quadrupled
  • squared
  • asBool

Contributing #

If you have read up till here, then πŸŽ‰πŸŽ‰πŸŽ‰. There are couple of ways in which you can contribute to the growing community of dart_extensions.dart.

  • Propose any feature, enhancement
  • Report a bug
  • Fix a bug
  • Participate in a discussion and help in decision making
  • Write and improve some documentation. Documentation is super critical and its importance cannot be overstated!
  • Send in a Pull Request :-)

Contributors ✨ #


Idan Ayalon

πŸ’» πŸ“– πŸ‘€

Xamantra

πŸ’» πŸ“– πŸ‘€

License #

Copyright 2020 Idan Ayalon
  
Licensed under the Apache License, Version 2.0 (the "License");  
you may not use this file except in compliance with the License.  
You may obtain a copy of the License at  
  
 http://www.apache.org/licenses/LICENSE-2.0  
Unless required by applicable law or agreed to in writing, software  
distributed under the License is distributed on an "AS IS" BASIS,  
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
See the License for the specific language governing permissions and  
limitations under the License.  
19
likes
100
pub points
70%
popularity

Set of method-extensions for dart that makes using framework in a much easier and clean way also adds additional functionality.

Repository (GitHub)
View/report issues

Documentation

Documentation
API reference

Uploader

bar.idan@gmail.com

License

BSD (LICENSE)

Dependencies

flutter, http, intl, quiver

More

Packages that depend on dart_extensions