final class OpenWeather {
final String baseURL, apiKey, baseWeatherIconUrl;
const OpenWeather._({required this.baseURL, required this.apiKey, required this.baseWeatherIconUrl});
static OpenWeather? _instance;
static Future<void> init() async {
if (_instance != null) return;
try {
await dotenv.load(fileName: 'my.env');
_instance = OpenWeather._(
baseURL: dotenv.get('BASE_API_URL'),
apiKey: dotenv.get('API_KEY'),
baseWeatherIconUrl: dotenv.get('BASE_WEATHER_ICON_URL'),
);
} catch (e) {
_instance = null;
throw Exception('Env file cannot be loaded or some required field was not found.');
}
}
static OpenWeather get instance {
if (_instance != null) return _instance!;
throw Exception('OpenWeather was not initialized.');
}
}
I'm new with this package (and get_it too) so I'm getting a hard time about how to transform this class to use it with Injection package.
Another question is about how to notify other singletons to await to be created until an instance of OpenWeather exists (after init async method to be executed), example:
@singleton
class MyClass {
void myMethod() {
final value = OpenWeather.instance.baseURL;
...
<code using this value>
...
}
}
Try N° 1:
@singleton
final class OpenWeather {
final String baseURL, apiKey, baseWeatherIconUrl;
const OpenWeather({required this.baseURL, required this.apiKey, required this.baseWeatherIconUrl});
@factoryMethod
static Future<OpenWeather> init() async {
try {
await dotenv.load(fileName: 'my.env');
return OpenWeather(
baseURL: dotenv.get('BASE_API_URL'),
apiKey: dotenv.get('API_KEY'),
baseWeatherIconUrl: dotenv.get('BASE_WEATHER_ICON_URL'),
);
} catch (e) {
throw Exception('Env file cannot be loaded or some required field was not found.');
}
}
}
@singleton
class MyClass {
final OpenWeather openWeather;
void myMethod(this.openWeather) {
final value = openWeather.baseURL;
...
<code using this value>
...
}
}
When running dart run build_runner build --delete-conflicting-outputs, it seems to be all OK so to check it I wanted to print a singleton value to my console from the home file of my app:
Launching .../flutter_weather2/lib/main.dart on 192.168.56.101:5555 in debug mode...
✓ Built build/app/outputs/flutter-apk/app-debug.apk.
Connecting to VM Service at ws://127.0.0.1:37787/zwYCVaXC1xA=/ws
E/flutter ( 2324): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Exception: Env file cannot be loaded or some required field was not found.
E/flutter ( 2324): #0 OpenWeather.init (file:///.../flutter_weather2/lib/config/servers/open_weather_server.dart:25:7)
E/flutter ( 2324): <asynchronous suspension>
E/flutter ( 2324): #1 _GetItImplementation._register.<anonymous closure>.<anonymous closure> (package:get_it/get_it_impl.dart:1093:44)
E/flutter ( 2324): <asynchronous suspension>
E/flutter ( 2324): #2 FutureGroup.add.<anonymous closure> (package:async/src/future_group.dart:79:15)
E/flutter ( 2324): <asynchronous suspension>
E/flutter ( 2324):
Removing try..catch the error message is:
Launching .../flutter_weather2/lib/main.dart on 192.168.56.101:5555 in debug mode...
✓ Built build/app/outputs/flutter-apk/app-debug.apk.
Connecting to VM Service at ws://127.0.0.1:34433/Fep90TXZAEw=/ws
E/flutter ( 2266): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Bad state: This instance of the type OpenWeather is not available in GetIt If you have registered it as LazySingleton, are you sure you have used it at least once?
E/flutter ( 2266): #0 throwIf (package:get_it/get_it_impl.dart:7:18)
E/flutter ( 2266): #1 _GetItImplementation._findFactoryByInstance (package:get_it/get_it_impl.dart:1245:5)
E/flutter ( 2266): #2 _GetItImplementation.signalReady (package:get_it/get_it_impl.dart:1280:28)
E/flutter ( 2266): #3 new OpenWeather (file:.../flutter_weather2/lib/config/servers/open_weather_server.dart:12:11)
E/flutter ( 2266): #4 OpenWeather.init (file:.../flutter_weather2/lib/config/servers/open_weather_server.dart:19:14)
E/flutter ( 2266): <asynchronous suspension>
E/flutter ( 2266): #5 _GetItImplementation._register.<anonymous closure>.<anonymous closure> (package:get_it/get_it_impl.dart:1093:44)
E/flutter ( 2266): <asynchronous suspension>
E/flutter ( 2266): #6 FutureGroup.add.<anonymous closure> (package:async/src/future_group.dart:79:15)
E/flutter ( 2266): <asynchronous suspension>
E/flutter ( 2266):
Observations:
It seems these codes:
@Singleton(signalsReady: true)
final class OpenWeather {
...
@FactoryMethod()
static Future<OpenWeather> init() async {
...
And
@Singleton()
final class OpenWeather {
...
@FactoryMethod(preResolve: true)
static Future<OpenWeather> init() async {
...
do the same thing but I don't know because the README file offers very basic help which does not allow proper use of the package, so which would be the correct solution to this problem?
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions
I have this singleton:
I'm new with this package (and get_it too) so I'm getting a hard time about how to transform this class to use it with Injection package.
Another question is about how to notify other singletons to await to be created until an instance of OpenWeather exists (after
init
async method to be executed), example:Try N° 1:
When running
dart run build_runner build --delete-conflicting-outputs
, it seems to be all OK so to check it I wanted to print a singleton value to my console from the home file of my app:But I get this error message:
So Where/which is the problem?
Try N° 2:
But I get this error message:
Try N° 3:
But I get this error message:
Removing
try..catch
the error message is:Observations:
And
do the same thing but I don't know because the README file offers very basic help which does not allow proper use of the package, so which would be the correct solution to this problem?