With the get_it integration on, the generated Server class gets a register(GetIt getIt)
method that registers the server and every data source with get_it.
Enable it#
constructs:
- name: revali_client
options:
integrations:
get_it: true
The next revali dev adds get_it to the generated package's pubspec.yaml. If the server project already depends on
get_it, the generated package uses the same version. Otherwise the dependency is added without a version constraint. The app that uses the client needs its own
get_it dependency to call GetIt.
Use it#
import 'package:client/client.dart';
import 'package:client/interfaces.dart';
import 'package:get_it/get_it.dart';
void main() {
Server(baseUrl: Uri.parse('https://api.example.com/api'))
.register(GetIt.instance);
final users = GetIt.I<UserDataSource>();
}
What register does#
void register(GetIt getIt) {
getIt.registerSingleton(this); // as Server
getIt.registerLazySingleton(() => user); // as UserDataSource
// ...one lazy singleton per non-excluded controller
}
-
The
Serverinstance is registered as a singleton under its class name (Server, or yourserver_name). -
Each data source is registered as a lazy singleton under its interface type, so depend on
UserDataSource, notUserDataSourceImpl, and swap in a fake in tests by registering your own implementation instead. -
StorageandRevaliClientare not registered. Read them fromGetIt.I<Server>().storageand.client.
Calling register twice on the same GetIt throws, because get_it rejects duplicate registrations. In tests, call
GetIt.I.reset() between runs.