Flutter : error when i'm trying to parse a Map<String, String>

Viewed 32

i have a factory to convert a json file to a class

  Map<String, dynamic>? tokenProperties;

  factory Token.fromJson(Map<String, dynamic> map) {
  
    return Token(
        tokenProperties: map['properties']
      );
  }

i receive in "map" param (keys are dynamics, never the same keys and values.)

enter image description here

when i want to affect the value tokenProperties: map['properties'] i don't why but the quote disappear... see the exception:

enter image description here

The full class:

// Dart imports:
import 'dart:convert';

/// SPDX-License-Identifier: AGPL-3.0-or-later

// To parse this JSON data, do
//
//     final token = tokenFromJson(jsonString);

Token tokenFromJson(String str) => Token.fromJson(json.decode(str));

String tokenToJson(Token data) => jsonEncode(data.toJson());

String tokenToJsonForTxDataContent(Token data) {
  return jsonEncode(data.toJsonForTxDataContent());
}

class Token {
  Token(
      {this.address,
      this.genesis,
      this.name,
      this.id,
      this.supply,
      this.type,
      this.symbol,
      this.tokenProperties});

  String? address;
  String? genesis;
  String? name;
  String? id;
  int? supply;
  String? type;
  String? symbol;
  Map<String, dynamic>? tokenProperties;

  factory Token.fromJson(Map<String, dynamic> map) {
  
    return Token(
        address: map['address'],
        genesis: map['genesis'],
        name: map['name'],
        id: map['id'],
        supply: map['supply'] == null
            ? null
            : int.tryParse(map['supply'].toString()),
        type: map['type'],
        symbol: map['symbol'],
        tokenProperties: map['properties']);
  }

  Map<String, dynamic> toJson() => <String, dynamic>{
        'address': address,
        'genesis': genesis,
        'name': name,
        'id': id,
        'supply': supply,
        'type': type,
        'symbol': symbol,
        'properties': tokenProperties,
      };

  Map<String, dynamic> toJsonForTxDataContent() => <String, dynamic>{
        'name': name,
        'supply': supply,
        'type': type,
        'symbol': symbol,
        'properties': tokenProperties,
      };
}
0 Answers
Related