0

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();
  }
1
  • What you want exactly? converting PedidoNovo to json or what else? Commented May 22, 2021 at 19:07

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.