Flutter Error Post - Datatime

Asked

Viewed 35 times

0

The following situation is occurring in my project. I have a model that has a property like Datetime, in Tojson I pass the date to the json using toIso8601String, in case I just put the current date when pressing the button

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

And then at the repositories I ride the body and give a post

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

the date converted, stays this wayinserir a descrição da imagem aqui

But when giving the post returns the status 500 and the message "Cannot read JSON Property of type = "Tdatetime", invalid value.". So I took the body and I ran a test on Postman, and I realized that changing the date 2021-03-06T10:42:38.774478 to 2021-03-06T10:42:38 worked, so I assume that the error relates to that part of the millisecond (.774478). Would I have some way to get them out when it’s time to convert, or should I do something else ?

  • If the error is STATUS 500 after the post, probably the problem is in your API... You need to tidy up inside your API so it understands the TIMESTAMP type or cast to turn into date before posting

  • I understood, and how I could convert Datetime to Timestamp ?

1 answer

1


I don’t quite understand your code but so summarizing will only need intpackage to format

import 'package:intl/intl.dart';

main() {
  var now = new DateTime.now();
  var formatter = new DateFormat('yyyy-MM-dd');
  String formatted = formatter.format(now);
  print(formatted); // something like 2013-04-20
}

documentationclick me

Any doubt I’m here bro

Browser other questions tagged

You are not signed in. Login or sign up in order to post.