gpt4 book ai didi

json - Flutter json_serializable 生成的 json 代码在 Firebase 数据库中保存为字符串

转载 作者:IT王子 更新时间:2023-10-29 06:59:30 25 4
gpt4 key购买 nike

我正在尝试使用 flutter 中的 firebase 数据库。我制作了一个模型类,我正在通过插件 json_serializable 将其转换为 json 和从 json。模型类在硬编码的 json 数据上工作得很好但是当我试图将编码的数据保存到 firebase 数据库时,它被保存为一个字符串而不是 firebase 数据库中的键值对。下面是我转换它并保存它的代码

    List<Action> actions = [];
actions.add(new Action('This is label', 'mranuran.com'));
EgluCard egluCard = new EgluCard(true, true, "HI", "World", "Awesome",CardType.TYPE_1 , ["images"], 123, 125, "#FFFFFF", PlatformType.IOS, actions);
//print(json.encode(egluCard));
// var jsonString = '{"isInternal":true,"isPublished":true,"title":"HI","body":"World","caption":"Awesome","cardType":"TYPE_1","images":["images"],"expiry":123,"showAt":125,"bgColor":"#FFFFFF","platformType":"IOS","actions":[{"label":"This is label","url":"mranuran.com"}]}';
// Map<String,dynamic> cardMap = json.decode(jsonString);
// EgluCard egluCard = EgluCard.fromJson(cardMap);
// print(egluCard.actions[0].label);
var jsonString = json.encode(egluCard);
var trimmedString = jsonString.substring(0,jsonString.length);
print(trimmedString);
cardsDBRef.push().set(jsonString)
.then((_){
print('saved!!');
});

当我打印 json.encode(eGluCard) 时,它正在打印一个有效的 json,但是当它被保存到 firebase 中时,我从 firebase 得到以下信息。本来应该保存为键值对,结果保存为一个完整的字符串。

"{\"isInternal\":true,\"isPublished\":true,\"title\":\"HI\",\"body\":\"World\",\"caption\":\"Awesome\",\"cardType\":\"TYPE_1\",\"images\":[\"images\"],\"expiry\":123,\"showAt\": 125,\"bgColor\":\"#FFFFFF\",\"platformType\":\"IOS\",\"actions\":[{\"label\":\"这是标签\",\"url\":\"mranuran.com\"}]}"

这种方法有什么问题?下面分别是我的模型分类和它们生成的序列化程序

EgluCard.dart

import 'card_type.dart';
import 'platform_type.dart';
import 'action.dart';
import 'package:json_annotation/json_annotation.dart';

part 'eglu_card.g.dart';

@JsonSerializable()

class EgluCard {

bool isInternal;
bool isPublished;
String title;
String body;
String caption;
CardType cardType;
List<String> images;
int expiry;
int showAt;
String bgColor;
PlatformType platformType;
List<Action> actions;

EgluCard(this.isInternal,this.isPublished,this.title,this.body,this.caption,this.cardType,this.images,
this.expiry,this.showAt,this.bgColor,this.platformType,this.actions);

factory EgluCard.fromJson(Map<String, dynamic> json) => _$EgluCardFromJson(json);

Map<String, dynamic> toJson() => _$EgluCardToJson(this);

}

EgluCard 序列化器

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'eglu_card.dart';

// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************

EgluCard _$EgluCardFromJson(Map<String, dynamic> json) {
return EgluCard(
json['isInternal'] as bool,
json['isPublished'] as bool,
json['title'] as String,
json['body'] as String,
json['caption'] as String,
_$enumDecodeNullable(_$CardTypeEnumMap, json['cardType']),
(json['images'] as List)?.map((e) => e as String)?.toList(),
json['expiry'] as int,
json['showAt'] as int,
json['bgColor'] as String,
_$enumDecodeNullable(_$PlatformTypeEnumMap, json['platformType']),
(json['actions'] as List)
?.map((e) =>
e == null ? null : Action.fromJson(e as Map<String, dynamic>))
?.toList());
}

Map<String, dynamic> _$EgluCardToJson(EgluCard instance) => <String, dynamic>{
'isInternal': instance.isInternal,
'isPublished': instance.isPublished,
'title': instance.title,
'body': instance.body,
'caption': instance.caption,
'cardType': _$CardTypeEnumMap[instance.cardType],
'images': instance.images,
'expiry': instance.expiry,
'showAt': instance.showAt,
'bgColor': instance.bgColor,
'platformType': _$PlatformTypeEnumMap[instance.platformType],
'actions': instance.actions
};

T _$enumDecode<T>(Map<T, dynamic> enumValues, dynamic source) {
if (source == null) {
throw ArgumentError('A value must be provided. Supported values: '
'${enumValues.values.join(', ')}');
}
return enumValues.entries
.singleWhere((e) => e.value == source,
orElse: () => throw ArgumentError(
'`$source` is not one of the supported values: '
'${enumValues.values.join(', ')}'))
.key;
}

T _$enumDecodeNullable<T>(Map<T, dynamic> enumValues, dynamic source) {
if (source == null) {
return null;
}
return _$enumDecode<T>(enumValues, source);
}

const _$CardTypeEnumMap = <CardType, dynamic>{CardType.TYPE_1: 'TYPE_1'};

const _$PlatformTypeEnumMap = <PlatformType, dynamic>{
PlatformType.ANDROID: 'ANDROID',
PlatformType.IOS: 'IOS',
PlatformType.ANDROID_THING: 'ANDROID_THING',
PlatformType.ANY: 'ANY'
};

Action .dart

import 'package:json_annotation/json_annotation.dart';

part 'action.g.dart';

@JsonSerializable()

class Action {
String label;
String url;

Action(this.label,this.url);

factory Action.fromJson(Map<String, dynamic> json) => _$ActionFromJson(json);

Map<String, dynamic> toJson() => _$ActionToJson(this);
}

Action 序列化器

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'action.dart';

// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************

Action _$ActionFromJson(Map<String, dynamic> json) {
return Action(json['label'] as String, json['url'] as String);
}

Map<String, dynamic> _$ActionToJson(Action instance) =>
<String, dynamic>{'label': instance.label, 'url': instance.url};

PlatformTypeCardType枚举

最佳答案

好的结果是我必须在将字符串传递给 firebase 之前对其进行解码和编码。因此,将模型类保存到 firebase 的代码如下所示:

    List<Action> actions = [];
actions.add(new Action('This is label', 'mranuran.com'));
EgluCard egluCard = new EgluCard(true, true, "HI", "World", "Awesome",CardType.TYPE_1 , ["images"], 123, 125, "#FFFFFF", PlatformType.IOS, actions);
var jsonString = json.encode(egluCard);
print(json.decode(jsonString));
cardsDBRef.push().set(json.decode(jsonString))
.then((_){
print('saved!!');
});

关于json - Flutter json_serializable 生成的 json 代码在 Firebase 数据库中保存为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53562939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com