Flutter web with Hive data base

2,860

Hive does its job correctly, it is an embedded database and must have different databases for different browsers. If you need this information to be shared in the cloud you should look for another type of database. A cloud database would be the solution. There are free solutions among the google tools for example, or to create your API connecting to a Database SQL for example.

Share:
2,860
m.r.davari
Author by

m.r.davari

Updated on December 21, 2022

Comments

  • m.r.davari
    m.r.davari over 1 year

    I developed demo web app with Flutter and upload it on my server and I used Hive database to store some data on web app.

    Recently I found that when I open web app and store some data on it, if I use different browser again I can't see the previously stored data, it seems that Hive on Flutter web will store data somewhere on client side cache.

    I have 3 questions now:

    • Where is the location of hive database and how can I access it manually?

    • How can I fix this problem and store data on my server with Flutter web that every user could see the same data?

    • Should I use Dart for server side to achieve this goal? If yes, where can I start and find good documents?

    enter image description here

    enter image description here

    Here is my code to save and load data:

    void _initHiveDB() async {
        
            if (_isDBInited) {
              return;
            }
        
            if(!kIsWeb){
              final documentsDirectory = await Path_Provider.getApplicationDocumentsDirectory();
              Hive.init(documentsDirectory.path);
            }
        
            Hive.registerAdapter(ComplaintModelAdapter(), 0);
            _isDBInited = true;
        
          }
    
    
    
        Future<bool> saveNewComplaint(ComplaintModel complaintModel)async{
        
            try{
              if(_complaintBox==null||!_complaintBox.isOpen){
                _complaintBox = await Hive.openBox('Complaints');
              }
              else{
                _complaintBox = Hive.box('Complaints');
              }
              _complaintBox.add(complaintModel);
              return true;
            }
            catch(exc){
              
              return false;
            }
        
          }
    
    
        Future<List<ComplaintModel>> loadAllComplaints() async {
        try{
              if(_complaintBox==null||!_complaintBox.isOpen){
                _complaintBox = await Hive.openBox('Complaints');
              }
              else{
                _complaintBox = Hive.box('Complaints');
              }
              //Box<ComplaintModel> complaintBox = await Hive.openBox('Complaints');
              //Box<ComplaintModel> complaintBox = await Hive.box('Complaints');
              List<ComplaintModel> complaints = _complaintBox.values.toList();
              return complaints;
            }
            catch(exc){
              return null;
            }}