type_token 1.0.4

  • Readme
  • Changelog
  • Example
  • Installing
  • 69

type_token extend on dart Type for more helpful functions, like more easier way to determine what type it is, free transforms between Type and TypeToken and easily resolve generic type.

Usage #

Instance

There are 4 ways to get instance of TypeToken.

main(){
  TypeToken token;
  token = TypeToken.of(int); // int
  token = TypeToken.ofInt(); // int
  token = TypeToken.parse(10); // int
  token = TypeToken.ofName("int"); // int
}

Types

after got a instance, you can determine what type it is.

main(){
  token.isInt; // int
  token.isDouble; // double
  token.isString; // string
  token.isBool; // bool
  token.isList; // list
  token.isMap; // map
  token.isDynamic; // dynamic
  token.isVoid; // void
  token.isPrimitive; // int, double, bool and string
  token.isNotPrimitive; // is not primitive
  token.isNativeType; // int, double, bool, string, list and map
}

Transforms #

TypeToken can transform to native type, but it will throw error if it is not a native type.

main(){
  Type nativeType = token.nativeType;  
}

Generic types #

TypeToken support very easy way for resolving generic types.

main(){
  // generics
  // get all generic types
  token = TypeToken.ofMapByType(int, String); // Map<int, String>
  token.generics; // [int, String]

  // nested generic type
  TypeToken genericToken;
  token = TypeToken.ofListByToken(TypeToken.ofList<int>()); // List<List<int>>
  genericToken = token.generics[0].generics[0]; // int
  genericToken = token.firstGeneric.firstGeneric; // int
  genericToken = token[0][0]; //int
}

toString() #

main(){
  // to string
  token.typeName; // without generic type
  token.fullTypeName; // with full generic type
  token.toString(); // same with fullTypeName
}

1.0.0 #

  • Initial version, created by Stagehand

1.0.3 #

  • bug fixed

1.0.4 #

  • bug fixed and update readme

example/type_token_example.dart

import 'package:type_token/type_token.dart';

main() {
  TypeToken token;

  // ways to get a instance of TypeToken
  token = TypeToken.of(int); // int
  token = TypeToken.ofInt(); // int
  token = TypeToken.parse(10); // int
  token = TypeToken.ofName("int"); // int

  // get instance of TypeToken with generic
  Type listType = <int>[].runtimeType;
  token = TypeToken.of(listType); // List<int>
  token = TypeToken.ofList<int>(); // List<int>
  token = TypeToken.parse([1, 2, 3]); // List<int>
  token = TypeToken.ofName("List", [TypeToken.ofInt()]); // List<int>
  token = TypeToken.ofName2("List", [int]); // List<int>
  token = TypeToken.ofFullName("List<int>"); // List<int>

  // determine what type is
  token.isInt; // int
  token.isDouble; // double
  token.isString; // string
  token.isBool; // bool
  token.isList; // list
  token.isMap; // map
  token.isDynamic; // dynamic
  token.isVoid; // void
  token.isPrimitive; // int, double, bool and string
  token.isNotPrimitive; // is not primitive
  token.isNativeType; // int, double, bool, string, list and map

  // parse to native type
  // only native type can parse, or throw error
  Type nativeType = token.nativeType;

  // generics
  // get all generic types
  token = TypeToken.ofMapByType(int, String); // Map<int, String>
  token.generics; // [int, String]

  // nested generic type
  TypeToken genericToken;
  token = TypeToken.ofListByToken(TypeToken.ofList<int>()); // List<List<int>>
  genericToken = token.generics[0].generics[0]; // int
  genericToken = token.firstGeneric.firstGeneric; // int
  genericToken = token[0][0]; //int

  // to string
  token.typeName; // without generic type
  token.fullTypeName; // with full generic type
  token.toString(); // same with fullTypeName
}

Use this package as a library

1. Depend on it

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


dependencies:
  type_token: ^1.0.4

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:type_token/type_token.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
42
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
88
Overall:
Weighted score of the above. [more]
69
Learn more about scoring.

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

  • Dart: 2.7.1
  • pana: 0.13.5

Health suggestions

Format lib/src/type_token_base.dart.

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

Maintenance suggestions

The package description is too short. (-12 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.4.0 <3.0.0