1

我正在尝试集成配置单元数据库,因此可以使用离线功能。我正在从 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);
}

地点.g:

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);
4

0 回答 0