How to register multiple TypeAdapter in Flutter Hive?
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Hive.initFlutter();
if (!Hive.isAdapterRegistered(ModelOneAdapter().typeId)) {
Hive.registerAdapter(ModelOneAdapter());
}
if (!Hive.isAdapterRegistered(ModelTwoAdapter().typeId)) {
Hive.registerAdapter(ModelTwoAdapter());
}
runApp(const MyApp());
}
This is my code and I can't register the second one.
ModelTwoAdapter()
Related
I have this code which starts the app build in error but when hot reloaded app works again if I hot restart error occurs again .I have initialized the Box and app works just fine after hot reload but not hot restart or if built in debug mode.
main
void main() async {
await Hive.initFlutter();
Hive.openBox('mybox');
runApp(MyApp());
}
HomePage.dart
class _HomePageState extends State<HomePage> {
final _myBox = Hive.box('mybox');
void writeData() {
if (_keyText.text.isEmpty || _valueText.text.isEmpty) {
if (_keyText.text.isEmpty) {
result = 'Key cannot be null';
_keyTextNode.requestFocus();
} else {
result = 'Value cannot be null';
_valueTextNode.requestFocus();
}
} else {
_myBox.put(_keyText.text, _valueText.text);
result =
'${_keyText.text} : ${_valueText.text} has been saved to HiveBox';
emptyTextFields();
}
setState(() {
result = result;
});
}
the openBox is asynchronous, you need to wait for the box until it's opened before you use it, add await:
void main() async {
await Hive.initFlutter();
await Hive.openBox('mybox'); // add await
runApp(MyApp());
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();//add this line to
//ensure Initialized
await Hive.initFlutter();
await Hive.openBox('mybox');
runApp(MyApp());
}
WidgetsFlutterBinding
i was building an app with Flutter and Firebase and I encountered the following problem:
I have a firebase helper class with within all the methods I need to use firebase realtime database and I will call these from the main function.
this is the firebase helper class:
class FBHelper {
FBHelper._internal();
static final DatabaseReference database = FirebaseDatabase.instance.ref('/');
factory FBHelper() {
return FBHelper._internal();
}
Future<void> initializeFirebase() async {
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
print('firebase app initialized');
}
Future<void> callPost(dynamic o) async {
await database.set(o);
}
}
and this is my main
void main() async {
final _ = FBHelper();
WidgetsFlutterBinding.ensureInitialized();
await _.initializeFirebase();
await _.callPost({"master": "1"});
runApp(const MyApp());
}
if I mark as static the DatabaseReference database property there are no problems at all.
instead, if I avoid the static modifier I get this
FirebaseException ([core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp())
can someone explain me what I’m missing? do I have a wrong idea of how to use static?
I am a Beginner in flutter and i was learning hydratedBloc, i imported all the needed dependencies as in the tutorial i was following and i run into a problem where my HydratedBlocOverrrides.runZoned is marked as an error
[This is the screenshot containing the errorthis is the Bloc_Imports files containg the exported hydratedBloc](https://i.stack.imgur.com/fVTcR.png)
Why do i have this error?
I tried using HydratedBloc to locally store data,i imported hydratedBloc but this syntax was marked and error "Undefined Name" refering to this HydratedBlocOverrrides.runZoned
According to hydrated_bloc's Changelog in version 9.0.0. It has removed HydratedBlocOverrides
BREAKING: feat!: reintroduce HydratedBloc.storage and remove HydratedBlocOverrides (#3479)
upgrade to bloc: ^8.1.0
Therefore change your code from:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
final storage = await HydratedStorage.build(
storageDirectory: await getApplicationDocumentsDirectory());
HydratedBlocOverrides.runZoned(
() => runApp(const MyApp()),
storage: storage,
);
}
to
void main() async {
WidgetsFlutterBinding.ensureInitialized();
HydratedBloc.storage = await HydratedStorage.build(
storageDirectory: await getTemporaryDirectory(),
);
runApp(const MyApp());
}
I have a problem while working with pat_provider.
Error:
Unhandled Exception: Null check operator used on a null value
Code:
void main() async {
final appDocumentDirectory =
await path_provider.getApplicationDocumentsDirectory();
Hive.init(appDocumentDirectory.path);
runApp(MyApp());
}
For more, I am using the non-nullsafety app, Right now I want to take the path to hive. what would you advise in such a case. Or should I downgrade the path_provider version?
Just add two lines: import material.dart and add WidgetsFlutterBinding.ensureInitialized();
import 'package:flutter/material.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
final appDocumentDirectory =
await path_provider.getApplicationDocumentsDirectory();
Hive.init(appDocumentDirectory.path);
runApp(MyApp());
}
Resolved! I was doing hive the old way. The program worked when I called Hive.initFlutter(), not Hive.init(). By the way,
import 'package: hive_flutter / hive_flutter.dart';
and
import 'package: hive / hive.dart';
Don't forget to import both,
Current code:
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Hive.initFlutter();
runApp(CoozinCustomerApp());
}
The code is here. I add getTemporaryDirectory in main.but It make the screen white.
Future<void> main() async {
await getTemporaryDirectory();
runApp(MyApp());
}
Do this instead:
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized(); // new line
await getTemporaryDirectory();
runApp(MyApp());
}
Read more about the ensureInitialized method here : WidgetsBinding ensureInitialized