I have a simple dart class as follows:
import 'package:flutter/material.dart';
class UiUtils {
// TEMPORARY FOR UNIT TEST PURPOSES ONLY
int addition(int x, int y) {
return x y;
}
}
(Note: The above is a sample, the actual class does have more than that temp function.)
My pubspec.yml
file contains the following:
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
intl: ^0.17.0
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
get_it: ^7.1.3
provider: ^6.0.1
mockito: ^5.0.16
I have a dependency locator
file as such:
import 'package:get_it/get_it.dart';
import 'package:quiz_test/utils/UiUtils.dart';
GetIt dependencyLocator = GetIt.instance;
void setupDependencyLocator() {
//dependencyLocator.registerSingleton(() => UiUtils());
dependencyLocator.registerFactory(() => UiUtils());
}
Finally, in main.dart
I have the following:
void main() {
setupDependencyLocator();
runApp(MyApp());
}
(There is of course more code than this).
As it is displayed, the code works fine, however if I change the dependancy_locator file from the current factory method to the singleton instead (i.e. comment out one to enable the other) I get the following error:
[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: type '_ServiceFactory<() => UiUtils, void, void>' is not a subtype of type '_ServiceFactory<Object, dynamic, dynamic>' of 'value'
#0 _LinkedHashMapMixin.[]= (dart:collection-patch/compact_hash.dart)
#1 _GetItImplementation._register (package:get_it/get_it_impl.dart:844:35)
#2 _GetItImplementation.registerSingleton (package:get_it/get_it_impl.dart:587:5)
#3 setupDependencyLocator (package:quiz_test/utils/dependency_locator.dart:7:21)
#4 main (package:quiz_test/main.dart:13:3)
#5 _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:145:25)
#6 _rootRun (dart:async/zone.dart:1428:13)
#7 _CustomZone.run (dart:async/zone.dart:1328:19)
#8 _runZoned (dart:async/zone.dart:1863:10)
#9 runZonedGuarded (dart:async/zone.dart:1851:12)
#10 _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:141:5)
#11 _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.<…>
Can anyone please help me to understand why I cannot use the singleton call rather than the factory one? My thought process is that I do not need a unique instance of this class, which is what I believe factory will give me, I just need a single instance of it for any classes that require it.
Any help is greatly appreciated.
CodePudding user response:
I was able to resolve this by doing the following:
dependencyLocator.registerSingleton<UiUtils>(UiUtils());
So, my dependencyLocator class now looks like this:
import 'package:get_it/get_it.dart';
import 'package:quiz_test/utils/UiUtils.dart';
GetIt dependencyLocator = GetIt.instance;
void setupDependencyLocator() {
dependencyLocator.registerSingleton<UiUtils>(UiUtils());
}
I hope this helps someone else from getting stuck!