Flutter Tutorial for Beginners #27 - World Time API

preview_player
Показать описание
Hey ninjas, in this Flutter tutorial we'll take a look at the API we'll be using to get our time data - the World Time API.
----------------------------------------

🐱‍💻 🐱‍💻 Course Links:

🐱‍💻 🐱‍💻 Other Related Courses:

Рекомендации по теме
Комментарии
Автор

4:50 i swear you did that mistake on purpose to show us what could happen and explain to us the best way, you are a master in teaching

algeriennesaffaires
Автор

For those who want to experiment another time zone which has offset in minutes too, don't forget to add that too:


String offset1 = data['utc_offset'].substring(1, 3);
String offset2 = data['utc_offset'].substring(4, 6);

DateTime now = DateTime.parse(datetime);
now = now.add(Duration(hours: int.parse(offset1), minutes: int.parse(offset2)));
print(now);

MrDuda
Автор

SOLUTIONS OF ERRORS
1. http error(Unhandled Exception: Bad state: Insecure HTTP is not allowed by platform): write https instead of http
2. Uri error: get(Uri.parse("https:..."));
3. for +/- error(wrong hours for negative timezones): substring(0, 3) instead of (1, 3)

mehmet-kpuj
Автор

Need a teacher like you.
You are the Best.
This can be the best programming channel on youtube.

tyson
Автор

You don't need the offset anymore. The API ensures the date and time is correct at specific region. Just use the 'datetime' property and you're good to go. But if you want to use the datetime that has no offset, you can use the 'utc_datetime' property.

romualdomariano
Автор

I'm in a timezone with a negative offset (-03:00) and you need to have the sign in front of the offset otherwise is always a positive number. The + or - in the front dosen't seems to bother the int convertion.

String offset = data['utc_offset'].substring(0, 3);

Ancientfall
Автор

This is made unnecessarily difficult by the way the DateTime.parse() function works. The 'datetime' property actually contains the exact time you want, but includes the offset at the end. DateTime.parse() automatically adds the offset, resulting in a DateTime object that is always set to UTC. An alternate method is to just make a substring of the first 26 characters (i.e. remove the offset from the 'datetime' property) and then parse that string like so:
String datetime = data['datetime'];
DateTime now = DateTime.parse(datetime.substring(0, 26));

troydontigney
Автор

in 24 row code must be:
String offset = data['utc_offset'].substring(0, 3);
For negative utc_offset e. g. America/Chicago = '-06:00' if you substring(0, 2) you get +6

uranberisha
Автор

You are just amazing. I never knew i will learn flutter with ease. I stopped last year as it was quite confusing, just three days i came across this videos i can code effortlessly. Many thanks bro for the good job. You are a talented and amazing code coach.

sundaya.philips
Автор

There is some time zones that have a negative offset number, so the correct thing to do is to set substring(0, 3) otherwise it will only add the offset.

arisraymis
Автор

07:57 This is incorrect. You have to include the sign at position 0. Otherwise for Timezones with negative (-) like Los Angeles (-08:00) it will add to the current time and the time will be incorrect. Still a great series and I love it.

fruitfcker
Автор

Now there is an easier way to add the offset, simply by just running:
`DateTime date =
it will automatically add the offset to the date

(add .toLocal() method to the DateTime object)

rankddum
Автор

I have found out that there may be an error when running the program with the end link, from World Time API, starting with 'http'. Rather it should start with 'https'. I had this error and could not figure out what was the issue until I came across this info.

jlatham
Автор

You have a very unique style of putting forward things. Recently stumbled upon your channel for some react concepts but ended up looking for other topics as well. Thanks for the videos..
2 things,
1. The response from the API your referring to already has utc_datetime and datetime variable so if we had to go forward with the offset addition and subtraction method then one should use utc_datetime response variable(This is obviously for other viewers)
2. I guess offsets can be in negative as well so again for other viewers one should take into consideration the addition and subtraction symbol before the offset amount. Or rather use a library which will help you increase or decrease the datetime with relevant timezone offsets.
Once again thanks for such informative videos.

vinayindoria
Автор

For anyone having issues with `Unhandled Exception: FormatException: Unexpected character (at character 1)`

What's likely failing is when we're trying to decode the body since we're assuming the body is a json format.
The issue is when we retrieve the response and there was an error from the server, the body will be an html format instead and confuse the json decoder.
So we can check the response's status code first, and only proceed if it's 200, otherwise it's an error and we can print it out.

For my case sometimes I did get 200 and everything worked ok, but other times the response was returning 503 (Service Unavailable).
This is not really our fault, just an issue on the server's side.

So you can check if it's a bad response then throw or print the error:

if (response.statusCode != 200) {
throw("Bad response ${response.statusCode}: ${response.body}");
}

jasonlabbed
Автор

Thanks for the tutorials mate.
I was planning on building an app for my business for a long time, but now I can definitely do it

jaigarg
Автор

If you want UTC negative offsets get the whole string included inthe + - signs when parsing offset

String datetime = data['datetime'];
int offset = int.parse(data['utc_offset'].substring(0, 3));
DateTime now = DateTime.parse(datetime);
now = now.add(Duration(hours: offset));
print(now);
}

PedroOjeda
Автор

If you get Unhandled Exception: Bad state: Insecure HTTP is not allowed by platform:"
Just change "http" by "https".

jacobalvarez
Автор

// You forgot main thing here or maybe you skipped for us to learn which is Adding Minutes & Subtracting if it is Negative TimeZone like -08:30 or +05:30. I've done it by trial n error. LEARNED it though. Thanks

//Creating DateTime Object
DateTime now = DateTime.parse(datetime); //convert to readable D & T
String sign = fetchedData['utc_offset'][0]; // Knowing TimeZone + or -
// Time Add or Minus based on TimeZone + or -
if (sign == '+') {
now = now.add(Duration(
hours: int.parse(offsetHours), minutes: int.parse(offsetMins)));
} else {
now = now.subtract(Duration(
hours: int.parse(offsetHours), minutes: int.parse(offsetMins)));
}
print(now);

hardiklakhalani
Автор

At time 1:15, you share your Ip address. I don't know if you care about it, but I recommend blurring it out if you don't mind having the type of information out there.

juaninfante
visit shbcf.ru