Iury Pereira
Iury Pereira

Reputation: 15

Flutter convert data Iso8601

I am trying to make a post, but it is returning the error "Cannot read JSON property of type = "TDateTime", invalid value. So I went to check the date passed in my Tojson method, and I saw that it seems that the date is not changing, even using the toIso8601String method, is this correct ? enter image description here

Edit

looking again at the code, the conversion is actually correct, but what causes the error are the milliseconds, because I changed the date 2021-03-06T10:42:38.774478 for 2021-03-06T10:42:38 in the json and it worked, could you remove this 774478 before putting it in the body

Button press page(dATA is DateTime)

onPressed: () {                                
                                    controllerPV.pedidoVenda.dATA = new DateTime.now();
                                    controllerPV.pedidoVenda.cANCELADO = 'N';
                                    controllerPV.pedidoVenda.mSGADICIONAL = controllerMsgEdicional.text;
                                    controller.submit();                 
                                  }

Repository

Parametros parametros = Parametros();
    await parametros.buscarParametros();

    var body = json.encode(
        pvenda.toJson(),
    );

    http.Response response = await http.post(
      parametros.url_api + URL_PVendasInserir,
      headers: {
        "Content-type": "application/json; charset=UTF-8"
      },
      body: body
    );

Upvotes: 0

Views: 646

Answers (1)

rkdupr0n
rkdupr0n

Reputation: 703

A function to show everything except the milliseconds:

String getNewDateTime(DateTime date) {
  final String y = date.year.toString();
  final String m = date.month.toString();
  final String d = date.day.toString();
  final String h = date.hour.toString();
  final String min = date.minute.toString();
  final String sec = date.second.toString();
  return "$y-$m-${d}T$h:$min:$sec";
}

Upvotes: 1

Related Questions