将Map <String,dynamic>数据转换为类数据

时间:2019-09-03 10:56:29

标签: flutter dart

在我的应用程序和一个屏幕中,我将带有Map<String,dynamic>的类数据传递到另一个屏幕,

onTap: (){
  Map<String,dynamic> dataMap = Map<String,dynamic>();
  dataMap['id']=tickets[index].id;
  dataMap['subject']=tickets[index].subject;
  dataMap['createdAt']=tickets[index].createdAt;
  dataMap['ticketNumber']=tickets[index].ticketNumber;

  Navigation.router.navigateTo(context, 'showTicketReplies/$dataMap',transition: TransitionType.fadeIn);
  },

在另一个屏幕上,我可以使用以下结构获取此数据:

widget.ticketData = {_InternalLinkedHashMap} size = 1
 0 = {map entry} "id" -> [_GrowableList]
  key = "id"
  value = {_GrowableList} size = 1
   0 = "{
           id: 1, 
           subject: hello world, 
           createdAt: 2019-01-01, 
           ticketNumber: 3381
        }"

现在,我想使用此数据制作class的简单变量:

TicketsTableData get _ticket => TicketsTableData.fromJson(widget.ticketData.values.toList()[0][0]);

widget.ticketData.values.toList()[0][0]的值是:

{id: 1, subject: hello world, createdAt:2019-01-01, ticketNumber: 3381}

但是当我尝试它时,出现此错误:

type 'String' is not a subtype of type 'Map<String, dynamic>'

更新

我认为我应该在制作课程数据之前将其转换为json。例如:

TicketsTableData get _ticket => TicketsTableData.fromJson(json.encode(widget.ticketData.values.toList()[0][0].toString()));

但是我得到json,但没有引号

1 个答案:

答案 0 :(得分:1)

问题解决了!

onTap: (){
  var dataMap ={};
  dataMap["id"]           = tickets[index].id;
  dataMap["subject"]      = tickets[index].subject;
  dataMap["createdAt"]    = tickets[index].createdAt;
  dataMap["ticketNumber"] = tickets[index].ticketNumber;

  Navigation.router.navigateTo(context, 'showTicketReplies/${json.encode(dataMap)}',transition: TransitionType.fadeIn);
},