4

我用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;
        }}
4

1 回答 1

1

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

于 2021-01-22T19:45:00.837 回答