Json 如何转换列表<;客户模型>;映射<;字符串,动态>;飘飘然

Json 如何转换列表<;客户模型>;映射<;字符串,动态>;飘飘然,json,flutter,dart,bloc,Json,Flutter,Dart,Bloc,我在《颤栗》中使用水合布洛克。为了使用HydreatedBloc,我需要将我的自定义列表(从JSON字符串解析)List转换为Map。下面是我用来解析json字符串的模型 import 'dart:convert'; List<List<Category>> rpTrendingCategoriesFromJson(String str) => List<List<Category>>.from(json.decode(str).map((

我在《颤栗》中使用水合布洛克。为了使用HydreatedBloc,我需要将我的自定义列表(从JSON字符串解析)
List
转换为Map。下面是我用来解析json字符串的模型

import 'dart:convert';

List<List<Category>> rpTrendingCategoriesFromJson(String str) => List<List<Category>>.from(json.decode(str).map((x) => List<Category>.from(x.map((x) => Category.fromJson(x)))));

String rpTrendingCategoriesToJson(List<List<Category>> data) => json.encode(List<dynamic>.from(data.map((x) => List<dynamic>.from(x.map((x) => x.toJson())))));

class Category {
  Category({
    this.title,
    this.imageLink,
  });

  String title;
  String imageLink;

  factory Category.fromJson(Map<String, dynamic> json) => Category(
    title: json["title"] == null ? null : json["title"],
    imageLink: json["imageLink"] == null ? null : json["imageLink"],
  );

  Map<String, dynamic> toJson() => {
    "title": title == null ? null : title,
    "imageLink": imageLink == null ? null : imageLink,
  };
}
导入'dart:convert';
List rpTrendingCategoriesFromJson(String str)=>List.from(json.decode(str.map)((x)=>List.from(x.map((x)=>Category.fromJson(x‘‘)));
字符串rpTrendingCategoriesToJson(List data)=>json.encode(List.from(data.map((x)=>List.from(x.map((x)=>x.toJson()));
类别{
类别({
这个名字,
这是imageLink,
});
字符串标题;
字符串图像链接;
工厂类别.fromJson(映射json)=>类别(
title:json[“title”]==null?null:json[“title”],
imageLink:json[“imageLink”]==null?null:json[“imageLink”],
);
映射到JSON()=>{
“title”:title==null?null:title,
“imageLink”:imageLink==null?null:imageLink,
};
}
这里我需要将
列表
转换为
地图

class TrendingCategoriesCubit扩展了Cubit
含水合糊精{
储存库;
TrendingCategoriesCubit({@required this.repository})
:super(TrendingCategoriesLoading());
Future fetchTrendingCategories()异步{
发射(TrendingCategoriesLoading());
列出类别=
等待repository.fetchTrendingCategories();
if(categories.isEmpty){
emit(TrendingCategoriesFetchedEmpty());
}else if(categories.isNotEmpty){
发出(TrendingCategoriesFetched(类别:类别));
}
}
@凌驾
TrendingCategoriesState fromJson(映射json){
试一试{
final categories=rpTrendingCategoriesFromJson(json.toString());
返回TrendingCategoriesFetched(类别:类别);
}接住{
返回null;
}
}
@凌驾
映射到JSON(趋势分类状态){
如果(状态为TrendingCategoriesFetched){
返回状态.categories.toJson();//无法执行此操作
}否则{
返回null;
}
}
}

如果您查看上面的
toJson()
方法,我需要将
List
返回到
Map一个简单的解决方法,以实现所需的结果,只需为水合块的toJson/fromJson部分创建一个映射:

@override
TrendingCategoriesState fromJson(Map<String, dynamic> json) {
  try {
    final categoriesJson = json['categories'];
    final categories = rpTrendingCategoriesFromJson(json.toString());
    return TrendingCategoriesFetched(categories: categories);
  } catch (_) {
    return null;
  }
}

@override
Map<String, dynamic> toJson(TrendingCategoriesState state) {
  if (state is TrendingCategoriesFetched) {
    return <String, dynamic>{
      'categories': state.categories.toJson(),
    };
  } else {
    return null;
  }
}
@覆盖
TrendingCategoriesState fromJson(映射json){
试一试{
最终分类json=json['categories'];
final categories=rpTrendingCategoriesFromJson(json.toString());
返回TrendingCategoriesFetched(类别:类别);
}接住{
返回null;
}
}
@凌驾
映射到JSON(趋势分类状态){
如果(状态为TrendingCategoriesFetched){
返回{
“categories”:state.categories.toJson(),
};
}否则{
返回null;
}
}

只需对代码进行一点调整即可编译,但总体思路是创建一个“临时”映射对象,仅用于保持和恢复水合状态。

谢谢您的回答。我会试着让你知道它是否有效
@override
TrendingCategoriesState fromJson(Map<String, dynamic> json) {
  try {
    final categoriesJson = json['categories'];
    final categories = rpTrendingCategoriesFromJson(json.toString());
    return TrendingCategoriesFetched(categories: categories);
  } catch (_) {
    return null;
  }
}

@override
Map<String, dynamic> toJson(TrendingCategoriesState state) {
  if (state is TrendingCategoriesFetched) {
    return <String, dynamic>{
      'categories': state.categories.toJson(),
    };
  } else {
    return null;
  }
}