dart_extensions 0.3.3

What New #

  • .withTooltip Tooltips improve the accessibility of visual widgets by proving a textual representation of the widget
  • .asBool for String and int
  • .inRangeOf - int
  • .any - Iterables
  • .groupBy - Iterables
  • .intersect - Iterables
  • .toMutableSet - Iterables

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

Flutter Extensions #

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

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]
}

.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.

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  

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

Http Extensions #

  • httpGet
  • httpPost
  • httpPut
  • httpDelete

Iterables Extensions #

  • 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

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.  

[0.3.3] 19/02/20

  • algorithm - quick search
  • Flutter - Tooltip extension

[0.2.0] - 23/01/20.

  • .groupBy() added Iterable + tests
  • .intersect() + test
  • .toMutableSet() + test

[0.1.8] - 22/01/20.

  • .filter() added Iterable + tests
  • .notFilter() added Iterable + tests
  • .isNullOrWhiteSpace added String + tests
  • .insert() added Strings + test

[0.1.7] - 19/01/202.

  • find - Iterable extensions + tests
  • added Ranges extension for int + tests

[0.1.4] - 19/01/202.

  • associate in Iterables extensions + tests
  • added Http Extensions: - httpGet httpPost httpPut httpDelete
  • added int Extensions: - absolute - isEven - isOdd - isPositive - isNegative - tenth - fourth - third - half - doubled - tripled - quadrupled - squared

[0.1.3] - 19/01/2020.

  • anyChar in Strings extensions + tests
  • replaceBefore Strings extensions + tests
  • toDoubleOrNull Strings extensions test
  • removeAllWhiteSpace Strings extensions + tests
  • toCharArray Strings extension + tests

example/lib/main.dart

/*
 * Copyright 2020 Idan Ayalon. All rights reserved.
 * 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.
 */

import 'package:dart_extensions/dart_extensions.dart';
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      routes: {"/login": (_) => Login()},
      title: 'Flutter Demo',
      theme: ThemeData(primarySwatch: Colors.blue),
      home: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Home(),
      ),
    );
  }
}

class Routes {
  static final login = "/login";
}

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

class _HomeState extends State<Home> {
  @override
  Widget build(BuildContext context) {
    /// For example purpose only

    return Scaffold(
      body: Container(
        child: Stack(
          children: <Widget>[
            Container(height: 100, width: 100)
                .withRoundCorners(backgroundColor: Colors.grey)
                .withShadow()
                .alignAtCenter()
                .toCenter()
                .paddingOnly(left: 10)
                .paddingAll(20)

                .onTap(
                  () async {
                    final result = await navigateByRouteName(Routes.login);
                    print(result);
                  },
                )  .withTooltip('just a tooltip')
                .onLongPress(() => print('long press'))

          ],
        ),
      ),
    );
  }
}

class Login extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.grey,
      child: Center(
        child: MaterialButton(
          child: Text('Go Back'),
          onPressed: () => navigateBack(context: context, result: "hello"),
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  dart_extensions: ^0.3.3

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:dart_extensions/dart_extensions.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
57
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
79
Learn more about scoring.

We analyzed this package on Feb 21, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.5
  • Flutter: 1.12.13+hotfix.7

Health suggestions

Format lib/src/date.dart.

Run flutter format to format lib/src/date.dart.

Format lib/src/flutter/container.dart.

Run flutter format to format lib/src/flutter/container.dart.

Format lib/src/flutter/navigation.dart.

Run flutter format to format lib/src/flutter/navigation.dart.

Fix additional 6 files with analysis or formatting issues.

Additional issues in the following files:

  • lib/src/flutter/padding.dart (Run flutter format to format lib/src/flutter/padding.dart.)
  • lib/src/flutter/widgets.dart (Run flutter format to format lib/src/flutter/widgets.dart.)
  • lib/src/http.dart (Run flutter format to format lib/src/http.dart.)
  • lib/src/iterable.dart (Run flutter format to format lib/src/iterable.dart.)
  • lib/src/search_algorithms.dart (Run flutter format to format lib/src/search_algorithms.dart.)
  • lib/src/strings.dart (Run flutter format to format lib/src/strings.dart.)

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.6.0 <3.0.0
flutter 0.0.0
http ^0.12.0+4 0.12.0+4
intl ^0.16.1 0.16.1
quiver ^2.0.0 2.1.2+1
Transitive dependencies
async 2.4.0
charcode 1.1.3
collection 1.14.11 1.14.12
http_parser 3.1.3
matcher 0.12.6
meta 1.1.8
path 1.6.4
pedantic 1.9.0
sky_engine 0.0.99
source_span 1.6.0
stack_trace 1.9.3
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test
test ^1.9.2