带有 Hive 数据库的 Flutter Web

m.r*_*ari 5 dart flutter flutter-dependencies flutter-web flutter-hive

我使用Flutter开发了演示Web应用程序并将其上传到我的服务器上,并使用Hive 数据库在 Web 应用程序上存储一些数据。

最近我发现,当我打开Web应用程序并在其上存储一些数据时,如果我再次使用不同的浏览器,我将看不到之前存储的数据,似乎Flutter Web上的Hive会将数据存储在客户端缓存的某个位置。

我现在有3个问题:

  • Hive 数据库的位置在哪里以及如何手动访问它?

  • 如何解决这个问题并使用 Flutter web 将数据存储在我的服务器上,以便每个用户都可以看到相同的数据?

  • 我应该在服务器端使用 Dart 来实现这个目标吗?如果是,我可以从哪里开始并找到好的文档?

在此输入图像描述

在此输入图像描述

这是我保存和加载数据的代码:

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;
        }}
Run Code Online (Sandbox Code Playgroud)

Fil*_*nio 1

Hive 正确地完成了它的工作,它是一个嵌入式数据库,对于不同的浏览器必须有不同的数据库。如果您需要在云中共享此信息,您应该寻找其他类型的数据库。云数据库将是解决方案。例如,谷歌工具中有免费的解决方案,或者创建连接到数据库 SQL 的 API。