Hello I'm currently building a Over Complicated Todo flutter app and I came across this error:
E/flutter (25502): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: 'package:get_it/get_it_impl.dart': Failed assertion: line 372 pos 7: 'instanceFactory != null': Object/factory with type DataServiceManager is not registered inside GetIt.
E/flutter (25502): (Did you accidentally do GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance;
E/flutter (25502): Did you forget to register it?)
but I clearly registered DataServiceManager in main.dart.
void main() {
WidgetsFlutterBinding.ensureInitialized();
void lazySingletonSetup() {
myLocator.registerLazySingletonAsync<TodoDatasource>(
() async => DataServiceManager()); // <<--------------- in question
}
lazySingletonSetup();
runApp(ChangeNotifierProvider(
create: (context) => TodoList(),
child: const TodoApp(),
));
}
I also tried using GetIt.I but error still persists.
also to take note my other files just in case:
DataServiceManager
class DataServiceManager implements TodoDatasource {
late final TodoDatasource _local;
final TodoDatasource _remote = RemoteApiDatasource();
DataServiceManager() {
if (kIsWeb) {
_local = LocalHiveDatasource();
} else {
_local = LocalSQLiteDatasource();
}
}
// TODO: REMOVE ALL COMMENTED BLOCKS OF CODE
// Future<bool> _checkConnectivity() async {
// // bool? result = html.window.navigator.onLine;
// // if ((kIsWeb && html.window.navigator.onLine == false) ||
// if (kIsWeb || await Connectivity().checkConnectivity() == ConnectivityResult.none) {
// return false;
// }
// return true;
// }
@override
Future<bool> addTodo(Todo t) async {
// return await _checkConnectivity()
// ? await _remote.addTodo(t)
// : await _local.addTodo(t);
return _local.addTodo(t);
}
@override
Future<bool> updateTodo(Todo t) async {
// return await _checkConnectivity()
// ? await _remote.updateTodo(t)
// : await _local.updateTodo(t);
return _local.updateTodo(t);
}
@override
Future<bool> deleteTodo(Todo t) async {
// return await _checkConnectivity()
// ? await _remote.deleteTodo(t)
// : await _local.deleteTodo(t);
return _local.deleteTodo(t);
}
@override
Future<List<Todo>> getAllTodo() async {
// return await _checkConnectivity()
// ? await _remote.getAllTodo()
// : await _local.getAllTodo();
return _local.getAllTodo();
}
@override
Future<List<Map<String, Object?>>> getId(int i) {
// return await _checkConnectivity()
// ? await _remote.getAllTodo()
// : await _local.getAllTodo();
return _local.getId(i);
}
}
TodoDatasource
import 'package:todo_list1/models/todo.dart';
abstract class TodoDatasource {
Future<List<Todo>> getAllTodo();
Future<bool> addTodo(Todo t);
Future<bool> deleteTodo(Todo t);
Future<List<Map<String, Object?>>> getId(int i);
// Future<Todo> getTodo(int i);
Future<bool> updateTodo(Todo t);
}
any help will be appreciated thank you.
CodePudding user response:
I have found the culprit. definitely the
myLocator.registerLazySingletonAsync<TodoDatasource>(() async => DataServiceManager());
is working but one of my provider class (TodoList) is looking for the registered GetIt, which is not registered. here's the sample codeblock:
class TodoList extends ChangeNotifier {
// This is our data model. which holds our data which is a list of To_do’s
List<Todo> _todos = [];
// This is an immutable returned list for presenting in the UI
UnmodifiableListView<Todo> get todos => UnmodifiableListView(_todos);
// constructor for the refresh method
TodoList() {
refresh();
}
// a total or court property getter
int get todoCount {
return _todos.length;
}
// GetIt method refresh
Future<void> refresh() async {
_todos = await GetIt.I<DataServiceManager>().getAllTodo();
notifyListeners();
silly me.
CodePudding user response:
If you are not passing an async factory function to registerLazySingletonAsync
use registerLazySingleton
instead.
void lazySingletonSetup() {
myLocator.registerLazySingleton<TodoDatasource>(() => DataServiceManager());
}