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

Bug: Recover the Injection by Inner Interface #873

Closed
Bwolfs2 opened this issue Aug 8, 2023 · 3 comments
Closed

Bug: Recover the Injection by Inner Interface #873

Bwolfs2 opened this issue Aug 8, 2023 · 3 comments
Labels
invalid This doesn't seem right new New issue request attention

Comments

@Bwolfs2
Copy link
Contributor

Bwolfs2 commented Aug 8, 2023

Describe the bug
I am migrating from Modular 5.0 to Modular 6.0 and I'm encountering the following issue: I need to retrieve a concrete injection through one of its extended interfaces. For instance, Auth extends both ISignin and ISignout, and I need to retrieve it using Modular.get().

To Reproduce

import 'package:flutter/material.dart';
import 'package:flutter_modular/flutter_modular.dart';
import 'package:flutter_modular/modular_interfaces/route/modular_route.dart';
import 'package:flutter_test/flutter_test.dart';

abstract class Signin {
  String signin();
}

abstract class Signout {
  String signout();
}

class Auth implements Signin, Signout {
  @override
  String signin() {
    return 'Signin';
  }

  @override
  String signout() {
    return 'Signout';
  }
}

void main() {
  testWidgets('Test Inner Inject', (tester) async {
    final modularApp = ModularApp(
      module: CustomModule(),
      child: const AppWidget(),
    );

    await tester.pumpWidget(modularApp);

    await tester.pump();
    expect(Modular.get<ValueNotifier>(), isNotNull);
  });

  testWidgets('Extends Class Inject', (tester) async {
    final modularApp = ModularApp(
      module: CustomModule(),
      child: const AppWidget(),
    );

    await tester.pumpWidget(modularApp);

    await tester.pump();
    expect(Modular.get<Auth>(), isNotNull);
    expect(Modular.get<Signin>(), isNotNull);
    expect(Modular.get<Signout>(), isNotNull);
    expect(Modular.get<Signin>().signin(), 'Signin');
    expect(Modular.get<Signout>().signout(), 'Signout');
  });
}

///MOCKS
class CustomModule extends Module {
  @override
  List<Bind> get binds => [
        Bind.singleton<ValueNotifier>((i) => ValueNotifier<int>(0)),
        Bind.singleton((i) => Auth()),
      ];

  @override
  List<ModularRoute> get routes => [
        ChildRoute('/', child: (_, __) => Container()),
      ];
}

class AppWidget extends StatelessWidget {
  const AppWidget({Key? key}) : super(key: key);

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

Expected behavior
When I use Modular.get(), it should return the Auth() implementation, but only with access to the function implemented in ISignin. I believe the test speaks for itself.

@Bwolfs2 Bwolfs2 added new New issue request attention bug Something isn't working labels Aug 8, 2023
@jacobaraujo7 jacobaraujo7 added invalid This doesn't seem right and removed bug Something isn't working labels Aug 8, 2023
@jacobaraujo7
Copy link
Contributor

This is expected behavior.
This could be considered a previous Bug. Now we have the possibility of registering several factories of the same instance.
Now the records are 1 to 1.

@Bwolfs2
Copy link
Contributor Author

Bwolfs2 commented Aug 8, 2023

Are you suggesting that I should do something like this? @jacobaraujo7

@override
 List<Bind> get binds => [
       Bind.singleton<ValueNotifier>((i) => ValueNotifier<int>(0)),
       Bind.singleton<Auth>(Auth.new),
       Bind.singleton<Signout >((i) => i.get<Auth>()),
       Bind.singleton<Signin>((i) => i.get<Auth>()),

     ];

@jacobaraujo7
Copy link
Contributor

Yes!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right new New issue request attention
Projects
None yet
Development

No branches or pull requests

2 participants