Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to fix time in queryParameters in flutter?

I want to make a get request with DateTime inside query parameters, but DateTime takes not that form that I want. How to fix that?

I want to set this currentDate to query parameter

final currentDate = DateFormat('yyyy-MM-ddTHH:mm:ss').format(DateTime.now().toLocal());
log(currentDate); //from=2022-12-06T16:17:46

But I get this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

final data = await _apiService.getNfbByPeriod(from: currentDate, to: DateTime(2022), token: accessToken!);
// currentDate = 2022-12-06T16%3A17%3A46

// /sessions/avg-nfb-by-period?from=2022-12-06T16%3A17%3A46&to=2022-01-01+00%3A00%3A00.000

>Solution :

To fix the issue with the DateTime in the query parameters, you can try encoding the DateTime using the Uri.encodeComponent() method before passing it to the getNfbByPeriod() method. This will ensure that the DateTime is properly encoded and formatted in the query parameter.

Here is an example of how you can do that:

final currentDate = DateFormat('yyyy-MM-ddTkk:mm').format(DateTime.now().toLocal());
final encodedDate = Uri.encodeComponent(currentDate);

// Pass the encodedDate to the `getNfbByPeriod()` method
final data = await _apiService.getNfbByPeriod(from: encodedDate, to: DateTime(2022), token: accessToken!);

This should fix the issue with the DateTime in the query parameters. Let me know if you have any other questions.

To format the DateTime object as an ISO 8601 date string, you can use the DateFormat.ISO_8601 constant from the dart:core library. Here is an example of how you can do that:

final currentDate = DateFormat.ISO_8601.format(DateTime.now().toLocal());
final data = await _apiService.getNfbByPeriod(from: currentDate, to: DateTime(2022), token: accessToken!);

This will ensure that the DateTime object is formatted as an ISO 8601 date string, which should be accepted by the API.

Alternatively, if you want to use a custom date format, you can use the DateFormat class to specify the desired format. Here is an example of how you can do that:

final currentDate = DateFormat('yyyy-MM-ddTkk:mm').format(DateTime.now().toLocal());
final data = await _apiService.getNfbByPeriod(from: currentDate, to: DateTime(2022), token: accessToken!);

In this case, the DateTime object will be formatted according to the specified format (‘yyyy-MM-ddTkk:mm’), which should also be accepted by the API.

I hope this helps. Let me know if you have any other questions.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading