Box not found. Did you forget to call Hive.openBox()? - flutter

When working with HIVE database in flutter. If you ever get error like this:
"Box not found. Did you forget to call Hive.openBox()?"
It means you haven't opened your box to
To resolve this issue call
await Hive.openBox("boxname");
before using the box

It means you haven't opened your box. To resolve this issue call
await Hive.openBox("boxname");
before using the box.

The box needs to be open either at the beginning, after database initialization or right before doing the operation on the box.
For example in my AppDatabase class I have only one box ('book') and I open it up in the initialize() method, like below:
The whole application and tutorial is here.
const String _bookBox = 'book';
#Singleton()
class AppDatabase {
AppDatabase._constructor();
static final AppDatabase _instance = AppDatabase._constructor();
factory AppDatabase() => _instance;
late Box<BookDb> _booksBox;
Future<void> initialize() async {
await Hive.initFlutter();
Hive.registerAdapter<BookDb>(BookDbAdapter());
_booksBox = await Hive.openBox<BookDb>(_bookBox);
}
Future<void> saveBook(Book book) async {
await _booksBox.put(
book.id,
BookDb(
book.id,
book.title,
book.author,
book.publicationDate,
book.about,
book.readAlready,
));
}
Future<void> deleteBook(int id) async {
await _booksBox.delete(id);
}
Future<void> deleteAllBooks() async {
await _booksBox.clear();
}
}

You have to open the box you want to use and make sure to use await while using the openBox() function.
await Hive.openBox("boxname");

Related

Flutter Testing LateInitializationError in my class?

I have a class that helps me handle the sharedpreferences:
class SharedPref {
static late final SharedPreferences prefs;
static initialize() async {
prefs = await SharedPreferences.getInstance();
}
SharedPref._();
static Future<void> save(String key, String value) async {
await prefs.setString(key, value);
}
// more methods that help me to save/load values...
}
Then I have a testing function:
class MockNavigatorObserver extends Mock implements NavigatorObserver {}
void main() {
testWidgets('Button is present and triggers navigation after tapped',
(WidgetTester tester) async {
final mockObserver = MockNavigatorObserver();
await tester.pumpWidget(
MaterialApp(
home: FirstPage(),
navigatorObservers: [mockObserver],
),
);
expect(find.byType(RaisedButton), findsOneWidget);
await tester.tap(find.byType(RaisedButton));
await tester.pumpAndSettle();
expect(find.byType(MyDetailedPage), findsOneWidget);
});
}
Whenever I run the test:
The following LateError was thrown running a test:
LateInitializationError: Field 'prefs' has not been initialized.
How am I supposed to initialize that? I called the SharedPref.initialize(); before the final mockObserver... but made no change.
Thanks in advance.
I called the SharedPref.initialize(); before
You need to await the call, otherwise you have race conditions when your save is called and initalize may or may not already be finished.
As you mentioned problem was in
final mockObserver = MockNavigatorObserver();
not inside main() function.
But it was another silly mistake with this kind error if wright initialisation like this:
late final MockNavigatorObserver mockObserver;
setUp(() {
mockObserver= MockNavigatorObserver();
})
Problem in this case is using final keyword. If use like that, only one test will pass. You need not using final keyword, instead:
late MockNavigatorObserver mockObserver;
setUp(() {
mockObserver= MockNavigatorObserver();
})

Flutter - How to add and retrieve data to/from hive?

I know it sounds simple and I went through the example given in the documentation. Yet somehow I am unable to get it right.
This is what I have:
void main() async {
await Hive.initFlutter();
//Hive.openBox('workoutBox');
runApp(const MyApp());
}
...
Next Screen:
var box;
...
Trying to add to the box
Future<void> _save() async{
// save doc id somewhere
final Id = doc.id;
//box = await Hive.openBox('workoutBox');
box.put("Id", Id);
}
Trying to retrieve in another function:
var someId = box.get("Id");
Current error: get was called on null
My confusion is, where/how do you declare, open and retrieve from the box in this situation?
It seems you are forgetting to initialize a Box param and assign the value returned by the openBox function to it.
After Hive initialization you should have something like this:
Box<myValue> boxValue = await Hive.openBox("myKey");
Important: the retrieval method will dependend based on what you need to do and, more importantly, how you saved your data in the first place.
Let's say you saved data like this:
await boxValue.add(value);
By adding data like this, the key assigned to the value will be an auto-incremented one, so that trying to retrieve it with a specific key that never was assigned in the first place will fail.
If you did add the data like this:
await boxValue.put("myKey", value);
then you will be able to successfully fetch it using the intended key.
You can do the following:
void main() async {
await Hive.initFlutter();
await Hive.openBox('workoutBox'); //<- make sure you await this
runApp(const MyApp());
}
...
_save() { // <- can be a synchronous function
final box = Hive.box('workoutBox'); //<- get an already opened box, no await necessary here
// save doc id somewhere
final Id = doc.id;
box.put("Id", Id);
}
I have written an example app and a Flutter Cubits + Hooks + Hive DB tutorial. I have the following AppDatabase class there:
const String _bookBox = 'book';
#Singleton()
class AppDatabase {
AppDatabase._constructor();
static final AppDatabase _instance = AppDatabase._constructor();
factory AppDatabase() => _instance;
late Box<BookDb> _booksBox;
Future<void> initialize() async {
await Hive.initFlutter();
Hive.registerAdapter<BookDb>(BookDbAdapter());
_booksBox = await Hive.openBox<BookDb>(_bookBox);
}
Future<void> saveBook(Book book) async {
await _booksBox.put(
book.id,
BookDb(
book.id,
book.title,
book.author,
book.publicationDate,
book.about,
book.readAlready,
));
}
Future<void> deleteBook(int id) async {
await _booksBox.delete(id);
}
...

How do I resolve a Future<File>, transform it to File, and ensure it's available before runApp starts?

My apologies for what I assume is rather basic question, but I'm struggling to understand this. I'm aware of What is a Future and how do I use it? but I don't think that applies in this case, or if it does I'm even more confused than I thought!
I'm attempting to use FileOutput in the Logger package to log to device storage. This requires a File object as a parameter.
To obtain the correct path I'm using getApplicationDocumentsDirectory() from the path_provider package. This returns a Future which I can manipulate to a Future in an async function with await.
I'm unclear though how to extract a File from this and how to make sure that these objects are available to the logger before they are needed. Does this need to be done before I call runApp()? I assume I don't need to and shouldn't push async up to the main()?
This is where I am. x2() is a test function I can call successfully out of main() after invoking runApp() and gives me the correct results.
Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
print("directory: $directory");
return directory.path;
}
Future<File> get _localFile async {
final path = await _localPath;
print("path: $path");
return File('$path/logger.out');
}
var logger2 = Logger(
output: MultiOutput([
ConsoleOutput(),
FileOutput(
file: _localFile,
overrideExisting: true,
),
]),
printer: PrettyPrinter(
printBox: false,
printTime: true,
),
filter: ProductionFilter(),
);
void x2() async {
var f = await _localFile;
print("_localFile: $f");
}
You should be able to set the Logger before the runApp but you don't need to await for it you can create a logger then access with a provider or a singleton instance and you can simple have a getLogger method that checks if the instance is available and if not calls the await overthere
this way you only call the future once and cache the instance in the class.
You may initialise your variable in x2() function.
Also, don't forget to await x2() as it will assure you that your instance has been created.
class Logger{
Logger({required this.number});
int number;
}
Future<int> get someNumber => Future.delayed(Duration(seconds:1),()=>5);
var logger;
Future<void> x2() async {
logger=Logger(number: await someNumber);
print(logger.number);
}
void main()async{
await x2();
print("main :: "+ logger.number.toString());
}
edit:
Also as #ahmetakil suggested, use provider or inherited widget if you need this instance down the widget tree

How To save and load an Object using SharedPreferences

So I have been trying to load all of my Recent Search history object using shared preferences.
and it doesn't work, it says that "List is not a subtype of List" the error code happens when I call the loadData function. I don't know why my code is not working.
So this is my shared preferences and method code:
void _addLastSearch(String hospitalId,String hospitalName)
{
final lastSearching=SearchModel(hospitalId: hospitalId,hospitalName: hospitalName);
setState(() {
recentSearch.add(lastSearching);
});
}
void savedData() async {
SharedPreferences prefs= await SharedPreferences.getInstance();
prefs.setString('data',jsonEncode(recentSearch));
}
void loadData()async{
SharedPreferences prefs= await SharedPreferences.getInstance();
print("test masul load");
recentSearch=jsonDecode(prefs.getString('data'));
print(recentSearch.length);
}
any im changin the load data function to:
recentSearch=(jsonDecode(prefs.getString('data'))).map((item)=>SearchModel.fromJson(item)).toList();

Flutter SharedPreferences getInstance return null

Although I set the _sharedPreferences in the constructor, it gets null in getUsername. I don't know missing what:
class PreferencesProvider {
SharedPreferences _sharedPreferences;
PreferencesProvider() {
SharedPreferences.getInstance().then((prefs) => _sharedPreferences = prefs);
}
String getUsername() {
return _sharedPreferences.getString("Username");
}
String getX() {
return _sharedPreferences.getString("X");
}
String getY() {
return _sharedPreferences.getString("Y");
}
String getZ() {
return _sharedPreferences.getString("Z");
}
}
alternatively it didn't work either:
class LoginProvider {
SharedPreferences _sharedPreferences;
LoginProvider._internal();
static final LoginProvider _instance = LoginProvider._internal();
factory LoginProvider() {
_instance.initPreferences();
return _instance;
}
initPreferences() async {
_sharedPreferences = await SharedPreferences.getInstance();
}
I want to use this in MaterialApp:
initialRoute: PreferencesProvider().isLoggedIn() ? "MainPage" : "LoginPage"
Edit: I know I should use await. But then keyword isn't same? I don't want to wait the instance again for all returns. In the other hand, I can't use await in initialRoute.
The way i manage to login the user for my application for the similar scenario is,
String startPage="LoginPage";
void main() {
SharedPreferences prefs = await SharedPreferences.getInstance();
String user=prefs.getString("Username");
if(user!=null && user.length>0){
startPage="MainPage";
}
runApp(MyApp());
}
Now, set your initialRoute as follow,
initialRoute: startPage,
This solution works in every scenario because i am fetching the data before the runApp() function in my application. Your application renders your initialPage after calling the runApp() function.
This is the best way to manage your login page based on data retrieval from the sharedpreferences as SharedPreferences takes time to fetch the data. Till the data is retrieved from sharedpreferences your build method gets completed its UI rendering.
While using preferences you should use Future, await and async
Future<String> getUsername() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String storeUserDetails = prefs.getString("Username");
return (storeUserDetails != null);
}
Hope this helps!
You need to wait a little bit for get username from shared preferences. getInstance is an async process.
Below code will work, because getString will work after getInstance
Future<String> getUsername() async {
_sharedPreferences = await SharedPreferences.getInstance();
return _sharedPreferences.getString("Username");
}
You need to modify your PreferencesProvider class