Boa tarde, pessoal,
Alguém saberia dizer o porquê de eu não conseguir capturar a injeção de dependência que estou fazendo nessa "SUB-SUB-MODULE" no FLUTTER_MODULAR?
class FlutterModularWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ModularApp(
module: AppModule(),
child: AppWidget()
);
}
}
APP WIDGET
class AppWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Modular - Examples',
).modular();
}
}
APP MODULE
class AppModule extends Module {
static const HOME_ROUTE = "/";
static const BIND_ROUTE = "/bind";
@override
List<Bind> get binds => [];
@override
List<ModularRoute> get routes => [
ChildRoute(HOME_ROUTE, child: (_, args) => HomePage()),
ModuleRoute(BIND_ROUTE, module: BindModule())
];
}
COUNTER (OBJETO QUE ESTARÁ SENDO FORNECIDO VIA INJEÇÃO DE DEPENDÊNCIA)
class Counter {
int _total = 0;
void add() => this._total += 1;
void sub() => this._total -= 1;
int get total => this._total;
void printTest() {
print("IM COUNTER! >> ${this.total}");
}
}
SUBMODULE 1
class BindModule extends Module {
static const SINGLETON_ROUTE = "/singleton";
@override
List<Bind> get binds => [];
@override
List<ModularRoute> get routes => [
ModuleRoute(SINGLETON_ROUTE, module: BindSingletonModule())
];
}
SUBMODULE 1-1
class BindSingletonModule extends Module {
static const BASE_PAGE = "/";
static const ONE_PAGE = "/one";
static const TWO_PAGE = "/two";
@override
List<Bind> get binds => [
Bind.singleton((i) => Counter())
];
@override
List<ModularRoute> get routes => [
ChildRoute(ONE_PAGE, child: (context, args) => BindSingletonOnePage())
];
}
ROTA (Que deseja capturar a injeção fornecida)
import 'package:examples/dependency_injection/flutter_modular/application/models/counter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_modular/flutter_modular.dart';
class BindSingletonOnePage extends StatefulWidget {
@override
_BindSingletonOnePageState createState() => _BindSingletonOnePageState();
}
class _BindSingletonOnePageState extends State<BindSingletonOnePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Center(child: Text("BIND - SINGLETON - ONE")),
),
body: Container(
padding: EdgeInsets.all(16.0),
child: Center(
child: Column(
children: [
modifyCounter(),
counter()
],
),
)),
);
}
Container counter() {
return Container(
child: Center(
child: Column(
children: [
Text("CONTADOR"),
Text("VALOR: ${1}"),
],
),
));
}
Container modifyCounter() {
return Container(
child: Center(
child: Column(
children: [
ElevatedButton(
child: Text("AUMENTAR"),
onPressed: () {
final Counter counter = Modular.get<Counter>();
counter.add();
counter.printTest();
}
),
ElevatedButton(
child: Text("DIMINUIR"),
onPressed: () {
final Counter counter = Modular.get<Counter>();
counter.sub();
counter.printTest();
}
)
],
),
));
}
}
PROBLEMA
- Quando tendo realizar o "Modular.get();", ele simplesmente diz que não existe.
- Mas se caso eu fornecer esse "Counter" no "BindModule" invés do "BindSingletonModule", a captura funciona.
DÚVIDA
- Porque isso acontece?
(encontrei esse fórum que faz mais sentido para minha dúvida)