dart_service_locator 1.0.5 copy "dart_service_locator: ^1.0.5" to clipboard
dart_service_locator: ^1.0.5 copied to clipboard

A lightweight, flexible dependency injection solution for Flutter applications, supporting both synchronous and asynchronous dependency resolution with minimal boilerplate.

example/lib/main.dart

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

class MyService {
  String fetchData() => 'Hello from MyService!';
}

void main() {
  // Register the service
  register<MyService>(() => MyService());

  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({super.key});

  @override
  Widget build(BuildContext context) {
    final myService = locate<MyService>();

    return Scaffold(
      appBar: AppBar(
        title: const Text('Service Locator Example'),
      ),
      body: Center(
        child: Text(myService.fetchData()),
      ),
    );
  }
}
0
likes
160
pub points
27%
popularity

Publisher

verified publisherdipendrasharma.com

A lightweight, flexible dependency injection solution for Flutter applications, supporting both synchronous and asynchronous dependency resolution with minimal boilerplate.

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

flutter

More

Packages that depend on dart_service_locator