Skip to content

Latest commit

 

History

History
177 lines (125 loc) · 5.79 KB

Day_08 Dependency injection.md

File metadata and controls

177 lines (125 loc) · 5.79 KB

Day 8 - Understanding Dependency Injection in Dart

What is Dependency Injection?

Dependency Injection (DI) is like a magical way of helping different parts of your code work together without being tightly connected. Imagine you have a toy car that needs batteries to run. Instead of building the batteries inside the toy car, you have a way to put batteries in when you need them. This way, you can change the batteries easily without changing the car itself. DI works similarly in your code. It allows you to inject (or provide) the necessary parts (dependencies) into your code when needed.

Day 8

Why Use Dependency Injection?

  • Flexibility: Makes it easy to swap parts without changing the whole system.
  • Testing: Allows for easy testing by injecting mock dependencies.
  • Maintenance: Improves code readability and maintenance by decoupling components.

Core Concepts of Dependency Injection

1. Dependencies

Dependencies are the parts or services your code needs to work. For example, if you have a class that handles user authentication, it might depend on a service that checks the user's credentials.

2. Injectors

Injectors are like the tools that provide the necessary parts to where they're needed. Think of an injector as someone who gives batteries to your toy car.

3. Providers

Providers define how to create the dependencies. They are like the instructions on where to get the batteries and how to put them in the toy car.

Setting Up Dependency Injection in Dart

Let's see how to set up DI in a Dart application. We'll use the provider package, a popular package for state management and DI in Flutter.

Step 1: Adding the Provider Package

First, add the provider package to your project. Open pubspec.yaml and add the dependency:

dependencies:
  flutter:
    sdk: flutter
  provider: ^6.0.0

Run flutter pub get to install the package.

Step 2: Creating a Simple Example Imagine we have a simple app where we want to greet the user. We'll create a GreetingService to handle the greeting message and use DI to inject this service into our app.

// greeting_service.dart
class GreetingService {
  String getGreeting() {
    return "Hello, Welcome to Dependency Injection!";
  }
}

Step 3: Setting Up the Provider Now, let's set up the provider in our app.

// main.dart
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'greeting_service.dart';

void main() {
  runApp(
    MultiProvider(
      providers: [
        Provider<GreetingService>(
          create: (_) => GreetingService(),
        ),
      ],
      child: MyApp(),
    ),
  );
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: GreetingScreen(),
    );
  }
}

class GreetingScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final greetingService = Provider.of<GreetingService>(context);

    return Scaffold(
      appBar: AppBar(title: Text('Dependency Injection Example')),
      body: Center(
        child: Text(greetingService.getGreeting()),
      ),
    );
  }
}

Explanation Provider: We use Provider to manage the creation and disposal of GreetingService. MultiProvider: Allows us to add multiple providers if needed. Consumer: We use Provider.of(context) to get the instance of GreetingService and use it in our GreetingScreen. Conclusion Dependency Injection is a powerful technique to make your code more modular, testable, and maintainable. By using DI, you can easily manage dependencies, swap implementations, and write better tests.

Full Example Output When you run the above code, you should see a screen displaying:

Hello, Welcome to Dependency Injection!

This simple example demonstrates how to use dependency injection in Dart using the provider package. You can find a more detailed explanation and additional examples in the GitHub repository.

Searching for Packages Packages are published to pub.dev. You can search for packages and add them to your project.

Adding a Package Dependency: To add a package dependency, follow these steps:

Depend on it: Open pubspec.yaml and add the package under dependencies. Install it: Run flutter pub get in the terminal. Import it: Add an import statement in your Dart code. Example: Using the owlbot_dart Package We'll create a Dart console dictionary that accepts a word and prints the definition using the owlbot_dart package.

Step 1: Adding the Package Add the owlbot_dart package to your pubspec.yaml:

dependencies:
  owlbot_dart: ^1.0.0

Run flutter pub get to install the package.

Step 2: Creating the Dictionary

// main.dart
import 'package:owlbot_dart/owlbot_dart.dart';

void main() async {
  final owlBot = OwlBot(token: 'YOUR_API_TOKEN');
  final response = await owlBot.define('flutter');

  print('Word: ${response.word}');
  print('Pronunciation: ${response.pronunciation}');
  for (var definition in response.definitions) {
    print('Definition: ${definition.definition}');
    print('Example: ${definition.example}');
  }
}

Explanation

OwlBot API: We use the owlbot_dart package to fetch word definitions from the OwlBot API. Asynchronous Code: The define method is asynchronous, so we use await to wait for the API response. Conclusion By understanding Dependency Injection and how to manage dependencies in Dart, you can create more modular, testable, and maintainable applications. Dependency management using pub and pubspec.yaml simplifies the process of adding and managing external packages.

#Dart #Flutter #DependencyInjection #Programming #SoftwareDevelopment #Coding #MobileDevelopment #FlutterDev #DartLang