Declar

Declar is a lightweight, declarative, and expressive utility library for Dart and Flutter. It provides powerful extensions, functional utilities, and result types that help you write elegant, SwiftUI-like declarative code that is clean, readable, and composable.


Features

  • String extensions — smart utilities like validation, casing, and transformation
  • Int and Double extensions — duration helpers, number formatting, and currency support
  • List utilities — unique filtering and transformations
  • Result & Maybe types — clean error handling and null-safety helpers
  • Functional helpers — condition builder, when expression, async typedefs
  • Declarative typedefs — standardized callback and async task signatures

Installation

Add Declar to your Dart or Flutter project:

dart pub add declar

Then import it:

import 'package:declar/declar.dart';

Examples

String Extensions

import 'package:declar/declar.dart';

void main() {
  print('test@example.com'.isEmail); // true
  print('123'.isNumeric); // true
  print('hello world'.capitalizeWords); // Hello World
  print('helloWorld'.snakeCase); // hello_world
  print('hello world'.kebabCase); // hello-world
  print('hello world'.truncate(5)); // hello...
  print('file.txt'.fileExtension); // txt
}

Int Extensions

print(2.isPrime); // true
print(3.seconds); // Duration(seconds: 3)

Double Extensions

print(123.456.toPrecision(2)); // 123.46
print(123.456.toCurrency()); // $123.46

List Extensions

print([1, 2, 2, 3, 1].unique); // [1, 2, 3]

Result & Maybe

final success = Result.ok(42);
final error = Result.err('Something went wrong');

print(success.isOk); // true
print(error.isErr); // true
print(success.unwrap()); // 42
// error.unwrap() throws "Something went wrong"

int? value;
print(value.unwrapOr(99)); // 99

int? nullable = 5;
print(nullable.map((x) => x * 2)); // 10

Condition Builder

final result = ConditionBuilder<String>()
    .when(false, 'first')
    .when(true, 'second')
    .otherwise('default')
    .build();

print(result); // "second"

Functional when Helper

final result = when(
  condition: true,
  then: () => 'True',
  otherwise: () => 'False',
);

print(result); // "True"

Typedefs Example

AsyncTask<int> fetchValue = () async => Result.ok(42);
final result = await fetchValue();
print(result.unwrap()); // 42

JSON json = {'name': 'Declar', 'version': 1};
print(json['name']); // Declar

Running Tests

This package includes unit tests for all utilities.

Run tests locally:

dart test

Example test import:

import 'package:declar/declar.dart';
import 'package:test/test.dart';

Folder Structure

lib/
 ├── declar.dart
 └── src/
     ├── extensions.dart
     ├── functions.dart
     ├── result.dart
     └── typedef.dart

License

This project is licensed under the MIT License. See the LICENSE file for details.


Author

Developed by Siva Sankar, Updown Interactive — building elegant, declarative tools for modern Flutter and Dart development. GitHub: https://github.com/updown-interactive

Libraries

declar