Skip to content

MarkLinovy/dart_try

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pub package

The Try control gives us the ability write safe code without focusing on try-catch blocks in the presence of exceptions.

Before

void main() {
  String? randomWord;

  try {
    randomWord = getRandomWordFromServer();
  } catch (e) {
    randomWord = 'fallback';
  }
}

With Try

void main() {
  final randomWord = Try.of(() => getRandomWordFromServer()).orElseGet('fallback');
}

Immutability

With try catch we will have to split initialization and declaration. This forces us to make variables mutable, although they might only get assigned a value once.

Null Safety

If we use try catch control flow we might have a different Variable type. Below we declare randomWord to be nullable. Now getRandomWordFromServer might return String? as well. We lose our strict null safety.

It's easier to read

Features

  • Wrap Code that might throw an error with Try.of(() => doSomething())
  • Get result immediately if sure there is no error
  • Return alternative the event of an error
    • tryable.orElse(() => elseFunction());
    • tryable.orElseGet('value')

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages