useful_extension 1.0.2+4

Dart native js
Flutter Android iOS web

Integrate other language code style to extensions.

useful_extension #

Integrate other languages (like Java and Kotlin) code style to extensions.

Dart Version

Java Util Function Extension #

Dart ClassJava ClassDart Prototype
Consumer<T>Consumer<T>void Function(T)
Predicate<T>Predicate<T>bool Function(T)
ActionActionvoid Function()
Supplier<R>Supplier<R>R Function()
Transformer<T, R>Function<T, R>R Function(T)
BiConsumer<T, U>BiConsumer<T, U>void Function(T, U)
BiPredicate<T, U>BiPredicate<T, U>bool Function(T, U)
BiTransformer<T, U, R>BiFunction<T, U, R>R Function(T, U)

Kotlin Style Extension #

OperatorParameterReturns
letselfself
apply-self
alsoselfR
run-R

Simple Usage #

String name = null;
name?.let((it){
  print("it=$it");
});

Because name is null, so let not execute.

String name = "Hello";
name?.let((it){
  print("it=$it");
});

Because name is not null, so outputs "it=Hello".

Use to Initialize Instance

By cascade style

People john = People()
  ..name = "John"
  ..age = 18;

By let operator

People john = People().let((it) {
  it.name = "John";
  it.age = 18;
});

Operator Chains

100.also((it) => it * it)
   .also((it) => it.toString())
   .also((it) => double.parse(it))
   .let((it) => print("result=$it"));

Functions Invocation

You can call invoke to execute a Function.

Dart style Function execution
Function func = () => print("Execute");
func();
Use invoke instead of Dart style execution
Function func = () => print("Execute");
func.invoke();
Use invoke and let to Simplified Null Safety Style

Dart style Function null safety

Function func = null;
if(func != null) func();

Instead of invoke and let style

Function func = null;
func?.invoke();
invoke also Supports Parameters and Returns

Parameters and returns will auto transform and keep type-safe.

typedef Func1 = bool Function(String s, int i);
typedef Func2 = String Function(int i);

Func1 func1 = null;
bool result1 = func1?.invoke("String", 100);

Func2 func2 = null;
String result2 = func2?.invoke(100);

func2?.invoke(true); /// compile error
bool r = func2?.invoke(100); /// compile errpr

Kotlin Collection Extension #

Iterable<E> #

Properties

PropertyTypeR/W
lastIndexintR
---
---
---

Methods

Return TypeMethod
Iterable<IndexedValue>withIndex()
Iterable<E>whereNot(bool test(E element))
Iterable<int>indexes()
EelementAtOr(int index, [E defaultValue = null])
EfirstOr([E defaultValue = null])
ElastOr([E defaultValue = null])

Dart Basic Extension #

Operator cast #

Use cast instead of as to make chain style.

dynamic o = "Hello";
(o as String).length;
o.cast<String>().length;

((o as String) as String).length;
o.cast<String>().cast<String>().length;

Futures #

3
likes
70
pub points
76%
popularity

Integrate other language code style to extensions.

Repository (GitHub)
View/report issues

Documentation

API reference

Uploader

meiswind711108@gmail.com

License

BSD (LICENSE)

More

Packages that depend on useful_extension