-
Notifications
You must be signed in to change notification settings - Fork 255
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
Comments
Correct: void binds(i){
i.addInstance(Config.new);
i.addInstance(ApiClient.new);
} |
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. |
Hi @Z3rolive, 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()}'),
],
),
),
);
}
}
|
Describe the question
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.
The text was updated successfully, but these errors were encountered: