This is my locator file
final getIt = GetIt.instance;
initGetIt() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
getIt.registerSingleton(() => Person());
getIt.registerSingleton(() => LocalStorage());
getIt.registerSingleton(() => preferences);
}
This is Local Storage file
class LocalStorage extends LocalStorageImpl {
SharedPreferences preferences = getIt<SharedPreferences>();
@override
void delete(String key) {
print("delete");
}
@override
dynamic getValue(String key) {
return preferences.get("Test");
}
@override
void save(String key, dynamic data) {
preferences.setString("Test", "Test");
}
}
abstract class LocalStorageImpl {
void save(String key, dynamic data);
dynamic getValue(String key);
void delete(String key);
}
This is main file
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await initGetIt();
runApp(MyApp());
}
This is how I calling it in widget
class CrudScreen extends StatefulWidget {
const CrudScreen({Key? key}) : super(key: key);
@override
State<CrudScreen> createState() => _CrudScreenState();
}
class _CrudScreenState extends State<CrudScreen> {
String? data;
LocalStorage storage = getIt<LocalStorage>();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Preference Screen'),
),
body: Center(
child: Text(data ?? ''),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
saveData();
},
),
);
}
void saveData() {
storage.save("key", "data");
data = storage.getValue("key");
setState(() {});
}
}
But I get this error
**Object/factory with type LocalStorage is not registered inside GetIt.
(Did you accidentally do GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance;
Did you forget to register it?)
'package:get_it/get_it_impl.dart':
Failed assertion: line 372 pos 7: 'instanceFactory != null'**
CodePudding user response:
I've got a working sharedPreference initialized through GetIt, but it requires some minor change with respect to your code, so I will propose you a solution suitable to your code and then if you want I can provide the code for the implementation that i'm sure will work
I think that it might be because you access preferences
in your local storage, before register the singleton, so I would suggest you to put getIt.registerSingleton(() => preferences
before getIt.registerSingleton(() => LocalStorage());
like this
initGetIt() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
getIt.registerSingleton(() => preferences);
getIt.registerSingleton(() => Person());
getIt.registerSingleton(() => LocalStorage());
}
EDIT: This is a solution that i'm sure is going to work, i'm sorry that it means to change slightly the logic of your code, but at least you will be able to adress the problem
void initGetIt() {
serviceLocator.registerSingleton<LocalStorage>(LocalStorage());
LocalStorage ls = serviceLocator.get<LocalStorage>();
ls.init();
...
}
class LocalStorage {
late SharedPreferences storage;
Future<void> init() async {
storage = await SharedPreferences.getInstance();
}
...
}
CodePudding user response:
Future<void> initializeGetIt() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
getIt.registerSingleton<SharedPreferences>(preferences);
getIt.registerSingleton<Person>(Person());
getIt.registerSingleton<LocalStorage>(LocalStorage());
}
This solved my error