集成 Hive 数据库

问题描述

我正在尝试集成一个 hive 数据库,因此可以使用离线功能。我正在从 Json 接收数据(标记信息),我可以将其成功保存在数据库中,并且当我有互联网连接时,它会在 Google 地图上显示标记

但是当设备离线时,该功能不再起作用。我在我的代码中发现了这个问题,但不知道如何解决

这是我的代码:(我调用“getToilets()”来接收信息)

地点:

import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:json_annotation/json_annotation.dart';
import 'package:hive/hive.dart';
import 'package:hive_Flutter/hive_Flutter.dart';
import 'package:path_provider/path_provider.dart';

part 'locations.g.dart';


@JsonSerializable()
class Toilet {
  Toilet({
    this.order,this.betrieb,this.strasse,this.ausstattung_wc,this.ausstattung_wickeltisch,this.ausstattung_barrierefrei,this.oeffnungszeiten,this.lat,this.lng,this.distance
  });

  factory Toilet.fromJson(Map<String,dynamic> json) => _$ToiletFromJson(json);
  Map<String,dynamic> toJson() => _$ToiletToJson(this);

  final String order;
  final String betrieb;
  final String strasse;
  final String ausstattung_wc;
  final String ausstattung_wickeltisch;
  final String ausstattung_barrierefrei;
  final String oeffnungszeiten;
  final double lat;
  final double lng;
  double distance;
}

@JsonSerializable()
class Locations {
  Locations({
    this.toilettenliste,});

  factory Locations.fromJson(Map<String,dynamic> json) =>
      _$LocationsFromJson(json);
  Map<String,dynamic> toJson() => _$LocationsToJson(this);

  final List<Toilet> toilettenliste;
}

Future<Locations> getToilets() async {
  await getAllData();
  

//everything works fine until I try to return the locations
  return Locations.fromJson(data.first); //<--

}

Box infoBox;
List data = [];

Future openBox() async {
  var dir = await getApplicationDocumentsDirectory();
  Hive.init(dir.path);
  infoBox = await Hive.openBox('data');
  return;
}

Future putData(data) async {
  await infoBox.clear();
  infoBox.add(data);
}

Future<bool> getAllData() async {
  await openBox();

  String url = 'https://...';

  try {
    var response = await http.get(url);
    var _jsonDecode = jsonDecode(response.body);
    await putData(_jsonDecode);
  } catch (SocketException) {
    print('No Internet!!!!!!!!!!!!!!!');
  }
  var mymap = infoBox.values.toList();
  if(mymap.isEmpty) {
    data.add('empty');
  } else {
    data = mymap;
  }
  return Future.value(true);
}

位置。例如:

part of 'locations.dart';



Toilet _$ToiletFromJson(Map<String,dynamic> json) {
  return Toilet(
      order: json['i'] as String,betrieb: json['b'] as String,strasse: json['s'] as String,ausstattung_wc: json['x'] as String,ausstattung_wickeltisch: json['y'] as String,ausstattung_barrierefrei: json['z'] as String,oeffnungszeiten: json['o'] as String,lat: double.parse(json['lat']),lng: double.parse(json['lng']));
}

Map<String,dynamic> _$ToiletToJson(Toilet instance) => <String,dynamic>{
  'order': instance.order,'betrieb': instance.betrieb,'strasse': instance.strasse,'ausstattung_wc': instance.ausstattung_wc,'ausstattung_wickeltisch': instance.ausstattung_wickeltisch,'ausstattung_barrierefrei': instance.ausstattung_barrierefrei,'oeffnungszeiten': instance.oeffnungszeiten,'lat': instance.lat,'lng': instance.lng
};

Locations _$LocationsFromJson(Map<String,dynamic> json) {
  print('Still running?!?!?!??!?!?!');
  return Locations(
      toilettenliste: (json['toiletten'] as List)
          ?.map((e) =>
      e == null ? null : Toilet.fromJson(e as Map<String,dynamic>))
          ?.toList());
}

Map<String,dynamic> _$LocationsToJson(Locations instance) =>
    <String,dynamic>{'toiletten': instance.toilettenliste};

当我从 Hive db 收到我的数据时,我收到此错误

[ERROR:Flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: type '_InternalLinkedHashMap<dynamic,dynamic>' is not a subtype of type 'Map<String,dynamic>'

有人知道为什么代码此时停止工作以及我该如何解决吗?

编辑:

我刚刚保存了来自 http.get 请求的响应,而不是 Locations.fromJson。

try {
    var responSEO = await http.get(url);
    await putData(responSEO.body);
  } catch (SocketException) {
    print('No Internet!!!!!!!!!!!!!!!');
  }

  var _jsonDecode = jsonDecode(infoBox.values.single);
  return Locations.fromJson(_jsonDecode);

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)