All Projects â†’ lesnitsky â†’ guard

lesnitsky / guard

Licence: MIT license
🛡try/catch with default value in case of exception or result is null

Programming Languages

dart
5743 projects

guard

lesnitsky.dev GitHub stars Twitter Follow

Evauluates the function wrapping the body with try/catch and returns default value if error was raised or return value is null

Installation

pubspec.yaml:

dependencies:
  guard: ^1.0.3

Example

import 'package:flutter/material.dart';
import 'package:guard/guard.dart';

class User {
  final String name;

  User(this.name);
}

void main() {
  final items = [];
  guard(() => items.first, null); // null

  User user;

  guard(() => user, 'No user found'); // No User found
  guard(() => Text(user.name), Text('Loading...')); // Loading...;

  asyncGuard(() async {
    await Future.error(null);
  }, 42); // Future(42);
}

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].