Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What is the new way to register bindings? #901

Open
Z3rolive opened this issue Sep 16, 2023 · 3 comments
Open

What is the new way to register bindings? #901

Z3rolive opened this issue Sep 16, 2023 · 3 comments
Labels
new New issue request attention question Questions about using some feature or general working of the package

Comments

@Z3rolive
Copy link

Z3rolive commented Sep 16, 2023

Describe the question

//Previously
List<Bind> get binds =>[
Bind<Config>((i)=> Config())
Bind<ApiClient>((i)=>ApiClient(
config: i<Config>()
))
]

//Current
void binds(i){
i.addInstance(Config());
i.addInstance(ApiClient(
config: i<Config>()
));
}

The code on previous worked on the previous version but the code on the current version doesn't work. What is the new way of registering bindings within a module. In this case there is one binding dependent on another.

Error I am getting is: Unregistered Binding : Config.

There is not enough documentation or example for this scenario.

@Z3rolive Z3rolive added new New issue request attention question Questions about using some feature or general working of the package labels Sep 16, 2023
@Z3rolive Z3rolive changed the title New way to register bindings. What is the new way to register bindings? Sep 16, 2023
@eduardoflorence
Copy link

Correct:

void binds(i){
  i.addInstance(Config.new);
  i.addInstance(ApiClient.new);
}

@Z3rolive
Copy link
Author

Thank you for the reply. This is probably the simplest scenario of all. Let's consider this scenario:

//Previously
List<Bind> get binds =>[
AsyncBind<Config>((i)=> Config.setup())
Bind<ApiClient>((i)=>ApiClient(
dio: i<DioOptionsSetup>().getInstance()
)),
]

In these cases, what would be the possible way to migrate? AsyncBind has been removed from the documentation and there is no alternative or the migration guide provided. There are a lot of changes between v5 to v6 but the documentation provided is less than the bare minimum.

@eduardoflorence
Copy link

Hi @Z3rolive,
Below is a complete example to simulate these dependencies:

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

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      routerConfig: Modular.routerConfig,
    );
  }
}

class Config {
  Config._();
  static final Config _instance = Config._();

  late String baseURL;
  late String token;

  Future<void> init() async {
    // Gets async settings
    await Future.delayed(const Duration(seconds: 2));
    baseURL = 'https://myapy.com';
    token = 'asdfg12345';
  }

  static Config setup() {
    return _instance;
  }
}

class Dio {}

class DioOptionsSetup {
  final Dio dio = Dio();
  // Dio configurations

  Dio getInstance() {
    return dio;
  }
}

class ApiClient {
  ApiClient({required this.dio});
  final Dio dio;
}

class AppModule extends Module {
  @override
  void binds(i) {
    i.add(() => Config.setup());
    i.add(DioOptionsSetup.new);
    i.add(() => ApiClient(dio: i<DioOptionsSetup>().getInstance()));
  }

  @override
  void routes(r) {
    r.child('/', child: (context) => const SplashPage());
    r.child('/home', child: (context) => HomePage(config: Modular.get<Config>()));
  }
}

class SplashPage extends StatefulWidget {
  const SplashPage({super.key});

  @override
  State<SplashPage> createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage> {
  @override
  void initState() {
    super.initState();
    Future(startServices);
  }

  Future<void> startServices() async {
    final config = Modular.get<Config>();
    await config.init();
    Modular.to.navigate('/home');
  }

  @override
  Widget build(BuildContext context) {
    return const Scaffold(
      body: Center(
        child: CircularProgressIndicator(),
      ),
    );
  }
}

class HomePage extends StatelessWidget {
  const HomePage({super.key, required this.config});

  // constructor dependency injection example
  final Config config;

  @override
  Widget build(BuildContext context) {
    // dependency example by service locator
    final apiClient = Modular.get<ApiClient>();
    return Scaffold(
      appBar: AppBar(title: const Text('Home Page')),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text('Base URL: ${config.baseURL}'),
            Text('Token: ${config.token}'),
            Text('ApiCliente: ${apiClient.toString()}'),
          ],
        ),
      ),
    );
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new New issue request attention question Questions about using some feature or general working of the package
Projects
None yet
Development

No branches or pull requests

2 participants