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());
}
Related
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 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());
}
I have a class where I am doing the graphql setup and the hive box setup. Here is the class -
class GraphQLConfiguration {
ValueNotifier<GraphQLClient> client = new ValueNotifier<GraphQLClient>(
GraphQLClient(
cache:
GraphQLCache(store: HiveStore(Hive.box(HiveStore.defaultBoxName))),
link: HttpLink('http://localhost:4000/graphql/',),
),
);
GraphQLConfiguration() {
initializeHive();
}
void initializeHive() async {
await initHiveForFlutter(); // or await initHiveForFlutter();
await Hive.openBox('bolBox');
}
}
Now I initialize this class in the Flutter main method -
Future main() async {
GraphQLConfiguration graphql = new GraphQLConfiguration();
}
When I run this code I get this error message -
Error - Unhandled Exception: HiveError: Box not found. Did you forget to call Hive.openBox()?
I followed this post as well Box not found. Did you forget to call Hive.openBox()?, didn't help.
Initialize Hive by giving it a home directory by using path_provider
final Directory appDocDir = await getApplicationDocumentsDirectory();
Hive.init(appDocDir.path);
then open box
await Hive.openBox('bolBox');
Add initHiveForFlutter in your root folder & it solve the problem.
void main() async{
await initHiveForFlutter();
runApp(MyApp());
}
Worked for me.
No need to initialise with open box & path as GraphQl handles that internally inside initHiveForFlutter.
Error: Unhandled Exception: HiveError: You need to initialize Hive or provide a path to store the box.
Essentially I have these in my dependencies so everything should be good.
hive: ^1.4.4+1
hive_flutter: ^0.3.1
path_provider: ^1.6.27
I also have import 'package:hive/hive.dart';
and
import 'package:path_provider/path_provider.dart'; in the file
So I just have
void doSomething() async {
final documentDirectory = await getApplicationDocumentsDirectory();
Hive.init(documentDirectory.path);
}
called.
I do not understand. I think I've done everything correct. Let me know if you need something else.
Hive needs to be initialized when it runs on Android or iOS, therefore you can use a function like this:
Future<Box> openHiveBox(String boxName) async {
if (!kIsWeb && !Hive.isBoxOpen(boxName))
Hive.init((await getApplicationDocumentsDirectory()).path);
return await Hive.openBox(boxName);
}
You'll need to import path_provider in order to access getApplicationDocumentsDirectory()
Try the following code on the main function of your flutter application:
import 'package:path_provider/path_provider.dart';
import 'package:hive/hive.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
final appDocumentDirectory = await getApplicationDocumentsDirectory();
Hive.init(appDocumentDirectory.path);
}
Currently, path_provider doesn't support WEB. You can see it here: path_provider.
You have to use another directory for WEB. If you are using BLOC as a state management, you could do something like this:
if (!kIsWeb) {
// if android or tablet
HydratedBloc.storage = await HydratedStorage.build(
storageDirectory: await getApplicationDocumentsDirectory(),
);
} else {
// if web
HydratedBloc.storage = await HydratedStorage.build(
storageDirectory: HydratedStorage.webStorageDirectory,
);
}
I got this error because of a typo:
await Hive.initFlutter;
should've been
await Hive.initFlutter();
I guess you are getting this issue because you are not awaiting the initFlutter.
import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:path_provider/path_provider.dart' as path_provider;
Future<void> yourFunction() async {
final dbDir = await path_provider.getApplicationDocumentsDirectory();
// init hive
await Hive.initFlutter(dbDir.path);
await openYourBox();
}
I think you should await your init method.
Actually you don't need use HydratedStorage to initialize Hive on web:
import 'package:hive/src/hive_impl.dart';
import 'package:flutter/foundation.dart';
import 'package:path_provider/path_provider.dart';
initializeHive()async{
//Use HiveImpl() to ensure you don't have conflicting Hive boxes.
HiveInterface _hive = HiveImpl();
if (kIsWeb) {
await _hive.openBox('yourBoxName');
} else {
var dir = await getApplicationDocumentsDirectory();
_hive.init(dir.path);
await _hive.openBox('yourBoxName');
}
}
If you're using Flutter on web, you don't need to initialize Hive and neither provider a path to box, only if you're using it on mobile.
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