nest_flutter 0.1.3 copy "nest_flutter: ^0.1.3" to clipboard
nest_flutter: ^0.1.3 copied to clipboard

Flutter integration for Nest Dart - bringing dependency injection and modular architecture to Flutter applications.

nest_flutter #

Flutter integration for Nest Dart - bringing dependency injection and modular architecture to Flutter applications.

Features #

  • 🎯 ModularApp Widget - Easy Flutter app initialization
  • πŸ”„ Provider Integration - Works with Flutter's widget tree
  • πŸ“± Reactive Services - ChangeNotifier support for state management
  • 🎨 Context Access - Get services from BuildContext
  • ⚑ Static API - Global service access with Modular class
  • 🚦 Modular Routing - go_router integration with module-based route organization
  • πŸ”— Route Prefixes - Organize routes with automatic path prefixing

Installation #

Add nest_flutter to your pubspec.yaml:

dependencies:
  flutter:
    sdk: flutter
  nest_flutter: ^0.1.3

Quick Start #

1. Create Your Module #

import 'package:nest_flutter/nest_flutter.dart';

class AppModule extends Module {
  @override
  void providers(Locator locator) {
    locator.registerSingleton<UserService>(UserService());
  }
  
  @override
  List<Type> get exports => [UserService];
  
  @override
  List<RouteBase> get routes => [
    GoRoute(
      path: '/',
      builder: (context, state) => HomePage(),
    ),
    GoRoute(
      path: '/users/:id',
      builder: (context, state) => UserDetailPage(
        userId: state.pathParameters['id']!,
      ),
    ),
  ];
}

2. Setup Your App with Router #

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

void main() {
  runApp(
    ModularApp(
      module: AppModule(),
      child: MyApp(),
    ),
  );
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      title: 'Nest Flutter Demo',
      routerConfig: Modular.router((router) {
        return GoRouter(
          routes: router.configuration.routes,
          initialLocation: '/',
        );
      }),
    );
  }
}

3. Use Services in Widgets #

class HomePage extends StatefulWidget {
  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  late final UserService _userService;
  
  @override
  void initState() {
    super.initState();
    _userService = Modular.get<UserService>();
  }
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Users')),
      body: FutureBuilder<List<User>>(
        future: _userService.getAllUsers(),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            return ListView(
              children: snapshot.data!
                  .map((user) => ListTile(
                    title: Text(user.name),
                    onTap: () => context.go('/users/${user.id}'),
                  ))
                  .toList(),
            );
          }
          return CircularProgressIndicator();
        },
      ),
    );
  }
}

Service Access Patterns #

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final userService = Modular.get<UserService>();
    // Use service...
  }
}

Context-Based Access #

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final container = Modular.containerOf(context);
    final userService = container.get<UserService>();
    // Use service...
  }
}

Reactive Services #

Create services that work with Flutter's reactive system:

class CounterService extends ChangeNotifier {
  int _count = 0;
  
  int get count => _count;
  
  void increment() {
    _count++;
    notifyListeners();
  }
}

// In your module
locator.registerSingleton<CounterService>(CounterService());

// In your widget
class CounterWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final counterService = Modular.get<CounterService>();
    
    return ListenableBuilder(
      listenable: counterService,
      builder: (context, child) {
        return Text('Count: ${counterService.count}');
      },
    );
  }
}

Advanced Usage #

Custom Container Provider #

ApplicationContainerProvider.withModules(
  modules: [AppModule(), UserModule()],
  child: MyApp(),
)

Service-Aware Base Widget #

abstract class ServiceAwareWidget<T extends StatefulWidget> extends State<T> {
  late final ApplicationContainer _container;
  
  @override
  void initState() {
    super.initState();
    _container = Modular.containerOf(context);
    onServicesReady();
  }
  
  void onServicesReady() {}
  
  S getService<S extends Object>() => _container.get<S>();
}

Testing #

Test widgets with mock services:

testWidgets('should display users', (tester) async {
  await tester.pumpWidget(
    ModularApp(
      module: TestModule(),
      child: MaterialApp(home: UserListWidget()),
    ),
  );
  
  expect(find.text('Test User'), findsOneWidget);
});

class TestModule extends Module {
  @override
  void providers(Locator locator) {
    locator.registerSingleton<UserService>(MockUserService());
  }
}

Platform Support #

  • βœ… Android - Full support
  • βœ… iOS - Full support
  • βœ… Web - Full support
  • βœ… macOS - Full support
  • βœ… Windows - Full support
  • βœ… Linux - Full support

Documentation #

Contributing #

We welcome contributions! Please see our Contributing Guide for details.

License #

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

2
likes
140
points
5
downloads

Publisher

verified publisherkhode.io

Weekly Downloads

Flutter integration for Nest Dart - bringing dependency injection and modular architecture to Flutter applications.

Homepage
Repository (GitHub)
View/report issues

Documentation

Documentation
API reference

License

MIT (license)

Dependencies

flutter, go_router, nest_core

More

Packages that depend on nest_flutter