useful_extension 1.0.0

  • Readme
  • Changelog
  • Installing
  • 59

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

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 #

1.0.0 #

  • Initial version, created by Stagehand

Use this package as a library

1. Depend on it

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


dependencies:
  useful_extension: ^1.0.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or 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:useful_extension/useful_extension.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
26
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
59
Learn more about scoring.

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

  • Dart: 2.7.1
  • pana: 0.13.6

Health issues and suggestions

Document public APIs. (-1 points)

36 out of 36 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Format lib/src/functions/functions.dart.

Run dartfmt to format lib/src/functions/functions.dart.

Format lib/src/kotlin_style/function_invokes.dart.

Run dartfmt to format lib/src/kotlin_style/function_invokes.dart.

Maintenance suggestions

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and useful_extension.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

The package description is too short. (-10 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0