所以我尝试向活动添加一个新对象并将其存储在本地存储中,代码如下所示 pushObjectWisata(selectedIndex, selectedObjectId) async { PengunjungModel dataObjectWi...
所以我试图向活动添加一个新对象并将其存储在本地存储中,代码如下所示
pushObjectWisata(selectedIndex, selectedObjectId) async {
PengunjungModel dataObjectWisata = listObjectWisata
.firstWhere((element) => element.id == selectedObjectId);
listAktifitas[selectedIndex].dataAktifitas!.add(dataObjectWisata);
await saveListAktifitas(listAktifitas);
}
但我收到此错误:
[错误:flutter/runtime/dart_vm_initializer.cc(41)] 未处理的异常:将对象转换为可编码对象失败:“listActivities”实例
listAktifitas 本身是一个具有多级对象的列表。模型如下所示
class listActivities {
String? namaAktifitas;
int? statusAktifitas;
int? countActivity;
String? thumbnail;
List<PengunjungModel>? dataAktifitas;
listActivities({
this.namaAktifitas,
this.statusAktifitas,
this.countActivity,
this.thumbnail,
this.dataAktifitas,
});
factory listActivities.fromJson(Map<String, dynamic> json) => listActivities(
namaAktifitas: json['namaAktifitas'].toString(),
countActivity: json['countActivity'],
statusAktifitas: json['statusAktifitas'],
thumbnail: json['thumbnail'].toString(),
dataAktifitas: List<PengunjungModel>.generate(
json['dataAktifitas'].length,
(index) => PengunjungModel.fromJson(
json['dataAktifitas'][index] ?? <String, dynamic>{}),
),
);
static List<listActivities> fromJsonList(dynamic jsonList) {
final listAktifitas = <listActivities>[];
if (jsonList == null) return listAktifitas;
if (jsonList is List<dynamic>) {
for (final json in jsonList) {
listAktifitas.add(
listActivities.fromJson(json),
);
}
}
return listAktifitas;
}
factory listActivities.fromRawJson(String str) =>
listActivities.fromJson(jsonDecode(str));
String toRawJson() => jsonEncode(toJson());
Map<String, dynamic> toJson() {
final data = <String, dynamic>{};
data['namaAktifitas'] = namaAktifitas;
data['statusAktifitas'] = statusAktifitas;
data['countActivity'] = countActivity;
data['thumbnail'] = thumbnail;
data['dataAktifitas'] = dataAktifitas;
return data;
}
}
如您所见,它与另一个列表 pengungungModel List 有关系。模型如下所示:
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:sbh_v2/model/pariwisata/foto.dart';
import 'package:sbh_v2/model/pariwisata/kategori_pengunjung.dart';
class PengunjungModel {
String? title;
int? id;
String? harga_terendah;
String? harga_tertinggi;
String? deskripsi;
String? jam_buka;
String? jam_tutup;
String? alamat;
String? fasilitas;
double? lat;
double? lng;
List<Foto>? list_foto = <Foto>[];
KategoriPengunjung? kategoriPengunjung;
PengunjungModel(
{this.id,
this.title,
this.harga_terendah,
this.harga_tertinggi,
this.deskripsi,
this.alamat,
this.list_foto,
this.jam_tutup,
this.jam_buka,
this.fasilitas,
this.lat,
this.lng,
this.kategoriPengunjung});
factory PengunjungModel.fromJson(Map<String, dynamic> json) =>
PengunjungModel(
id: json['id'],
title: json['title'] ?? "-",
alamat: json['alamat'] ?? "-",
jam_buka: json['jam_buka'] ?? "-",
jam_tutup: json['jam_tutup'] ?? "-",
fasilitas: json['fasilitas'],
lat: double.parse(json['lat'] ?? '0'),
lng: double.parse(json['lng'] ?? '0'),
harga_terendah: json['harga_terendah'] ?? "",
harga_tertinggi: json['harga_tertinggi'] ?? "",
deskripsi: json['deskripsi'] ?? "-",
list_foto: List<Foto>.generate(
json['foto'].length,
(index) => Foto.formJson(
json['foto'][index] ?? <String, dynamic>{},
),
),
kategoriPengunjung: KategoriPengunjung.fromJson(
json['kategori_layanan_pengunjung'] ?? <String, dynamic>{}),
);
factory PengunjungModel.fromRawJson(String str) =>
PengunjungModel.fromJson(jsonDecode(str));
}
基本上,我尝试将具有特定 id 的 pengunjungModel 数据对象添加到具有特定索引的 listAktifitas 中...
在 pushObjectWisata 中我尝试打印 selectedIndex 和 selectedObjectId 并且它工作正常。但是当我尝试打印 dataObjectWisata 时它返回该错误并且当我将 dataObjectWisata 添加到 listAktivitas 时也会发生同样的事情。
我不知道我不明白什么。但也许这是因为这两个的模型。
请帮帮我...谢谢