flutter_gate_generator

Code generation for flutter_gate policies and features via build_runner.


Keywords
feature-flags, flutter, flutter-package, policy
License
MIT

Documentation

flutter_gate

Authorization policies and feature flags for Flutter.

pub package License: MIT

Define who can do what with policies, toggle functionality with feature flags, and wire both into your UI with declarative widgets. Works with vanilla Flutter, Provider, Riverpod, Bloc, and GetX.

Features

  • Policy-based authorizationview, create, update, delete, and custom abilities per model
  • Feature flags — sync, async, observable, rollout percentage, and runtime overrides
  • Declarative widgetsCan, Cannot, FeatureGuard, and BuildContext extensions
  • Hooks — global before/after interceptors for admin bypasses and audit logging
  • Code generation — auto-register policies and features with build_runner
  • State management adapters — Provider, Riverpod, Bloc, GetX
  • Pure Dart coreflutter_gate_core has no Flutter dependency

Packages

Package Description
flutter_gate_core Core engine (pure Dart)
flutter_gate Flutter widgets and extensions
flutter_gate_provider Provider adapter
flutter_gate_riverpod Riverpod adapter
flutter_gate_bloc Bloc/Cubit adapter
flutter_gate_getx GetX adapter
flutter_gate_generator build_runner code generation

Installation

dependencies:
  flutter_gate: ^0.1.1

Optional adapters and generator:

dependencies:
  flutter_gate_riverpod: ^0.1.1

dev_dependencies:
  flutter_gate_generator: ^0.1.1
  build_runner: ^2.4.0

Quick start

1. Register policies and features at startup:

import 'package:flutter_gate/flutter_gate.dart';

void main() {
  gate.policy<Post>(() => PostPolicy());
  gate.define(const DarkModeFeature());
  runApp(const MyApp());
}

2. Define a policy:

class PostPolicy extends Policy {
  @override
  PolicyResponse update(dynamic user, dynamic model) {
    final post = model as Post;
    return PolicyResponse.fromBool(user == post.authorId);
  }
}

3. Guard UI with Can:

Can(
  ability: 'update',
  user: currentUserId,
  model: post,
  child: IconButton(icon: Icon(Icons.edit), onPressed: onEdit),
)

4. Toggle features with FeatureGuard:

FeatureGuard<DarkModeFeature>(
  child: DarkThemeSettings(),
  fallback: SizedBox.shrink(),
)

See the full example app for end-to-end usage.

Documentation

Guide Topics
Getting started Install, bootstrap, first policy & feature
Policies Abilities, responses, scoped context
Feature flags Sync, async, rollout, overrides
Widgets Can, FeatureGuard, extensions
Hooks Before/after authorization interceptors
Code generation @PolicyFor, @GateFeature, build_runner
State management Provider, Riverpod, Bloc, GetX
Testing Override gate, widget tests

Monorepo development

# Install melos
dart pub global activate melos

# Bootstrap all packages
melos bootstrap

# Analyze and test
melos run analyze
melos run test

License

MIT — see LICENSE.