Johnny
Johnny

Reputation: 419

How do I Convert String values to Json Type

i have a String and i need to convert it to json type, how can I do that?

[PedidoNovo(id: 2, mesa: 10, comanda: 22, codproduto: 309, produto: CALABRESA - G, quantidade: 1, preco_unitario: 48, preco_total: 0, produtoobservacao: , codpizza: 82352864)]

that's how I get the String.

it need to be like "id": 2 but I'm struggling to do that.

What I'm trying so far

  @override
  String toString() {
    return (StringBuffer('["PedidoNovo":{')
      ..write('"id": "$id", ')
      ..write('"mesa": "$mesa", ')
      ..write('"comanda": "$comanda", ')
      ..write('"codproduto": "$codproduto", ')
      ..write('"produto": "$produto", ')
      ..write('"quantidade": "$quantidade", ')
      ..write('"preco_unitario": "$preco_unitario", ')
      ..write('"preco_total": "$preco_total", ')
      ..write('"produtoobservacao": "$produtoobservacao", ')
      ..write('"codpizza": "$codpizza"')
      ..write(']'))
        .toString();
  }

Upvotes: 0

Views: 141

Answers (2)

Johnny
Johnny

Reputation: 419

Thank you guys, but that's what I needed:

    return (StringBuffer('{')
      ..write('"mesa": "$mesa", ')
      ..write('"comanda": "$comanda", ')
      ..write('"codproduto": "$codproduto", ')
      ..write('"produto": "$produto", ')
      ..write('"quantidade": "$quantidade", ')
      ..write('"preco_unitario": "$preco_unitario", ')
      ..write('"preco_total": "$preco_total", ')
      ..write('"produtoobservacao": "$produtoobservacao", ')
      ..write('"codpizza": "$codpizza"')
      ..write('}'))
     .toString();

Upvotes: 0

SilkeNL
SilkeNL

Reputation: 540

Try

 Map<String, dynamic> toJson() =>
  {
    "id": id,   //Your class properties
    "title": title,
    "description": description,
    "price": price,
  };

You can call it with myClass.toJson. I got this from How to encode an object to json in Flutter

Upvotes: 1

Related Questions