maybe_as 0.0.6 copy "maybe_as: ^0.0.6" to clipboard
maybe_as: ^0.0.6 copied to clipboard

Optional as. Returns null if type if not match the class type.

Info #

Optional as. maybeAs returns the current object if the type is the same and null otherwise
maybeAsOr returns the current object or default value
so it safe to use and avoid constructions like this:

dynamic object = 1; 
object is String ? object : null; 
object is String ? object :  "test"; 

Example #

import 'package:maybe_as/maybe_as.dart';

"1".maybeAs<String>() // -> "1"
1.maybeAs<String>() // -> null
"1".maybeAsOr<int>(0) // -> 0
1.maybeAsOr<String>("0") // -> "0"

with dynamic type

dynamic variable;
variable = 1;
(variable as Object).maybeAsOr<String>("0") // -> "0"
variable = "string";
variable.maybeAsOr<String>("0") // -> "string"
5
likes
140
pub points
42%
popularity

Publisher

verified publisherpub.iostech.org

Optional as. Returns null if type if not match the class type.

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-2-Clause (LICENSE)

Dependencies

flutter

More

Packages that depend on maybe_as