The timezone in Flutter: How To Work With Timezone In Flutter

Page 1

The timezone in Flutter: How To Work With Timezone In Flutter


The DartTime class - In the Dart language Dart allows us to do a lot of things with time from getting the system time to elements the time received from other sources but with the ever-changing list of timezones and daylight savings, the DateTime class is not aware with timezone. The Google team used the package of them called ‘timezone’ package solves it by making DateTime class timezone aware, which uses IANA time zone databases that make it reliable for use in production code.


Let’s have a look on below steps of How to use timezone with Flutter: 1. Add the Package into your project’s pubspec.yaml ● dependencies: ● timezone: ^0.5.4


2. Install the package ● Flutter pub get 3. In your terminal navigate to the package’s installation directory ● If you are using Android Studio then you will be able to navigate the directory just by going to the external libraries section under the project view. ● Go to dart packages and open timezone in the terminal. ● Once you open the terminal it will redirect to ‘lib’ folder of the package. ● But you need to go to one level up. ● cd...


4. Download the timezone databases ● To download the database from IANA the package comes with a handy script. ● You can find the latest version online and run the script inside the package directory using below command ● flutter pub run tool/get -s 2019b 5. Add database into your pubspec.yaml Assets: - packages/timezone/data/2019b.tzf


6.Enable the library â—? Before using the library the database needs to be loaded first and the Github page of the package suggested to do so in the main.dart file. â—? As in my project, I create a TimeHelperService hence I did the setup in the constructor of the class. import 'package:timezone/timezone.dart'; import 'package:flutter/services.dart'; class TimeHelperService { TimeHelperService() { setup(); }


void setup() async { var byteData = await rootBundle.load('packages/timezone/data/2019b.tzf'); initializeDatabase(byteData.buffer.asUint8List()); } } 7. Use the library void convertLocalToDetroit() async { DateTime indiaTime = DateTime.now(); //Emulator time is India time final detroitTime = new TZDateTime.from(indiaTime, getLocation('America/Detroit')); print('Local India Time: ' + indiaTime.toString()); print('Detroit Time: ' + detroitTime.toString()); }


Final Output:


flutter: Local India Time: 2019–09–08 15:25:44.858303 flutter: Detroit Time: 2019–09–08 05:55:44.858303–0400 So, here are the steps with an output that shows the use of timezone with flutter. There are a number of services that we can provide and here is a small list of our flutter development services ● Customized app development ● Enterprise mobile solutions ● Cross-platform app development ● Flutter widgets development ● Flutter app QA & Testing services ● Custom UI development


Concetto Labs - Flutter Development Company who have great experience in the industry. Just hire flutter app developer from us to build your next android and iOS apps with a single codebase in record time. Not just this, you can get the benefits of costsaving and willingness to compromise without any overheads.


Web & Mobile App Development Company

https://concettolabs.com


Global Network USA 4811 N Harding Ave, Chicago IL 60625 P: +1(903) 200-8801

UK 6 Hastings Avenue Ilford , IG6 1DZ P: +44(798)560 0352

CANADA 1445 Rennie St Oshawa, Ontario, L1K 0N9,CANADA P: (416) 272-0981

INDIA 609/610,City Center Science City Road Ahmedabad - 380060 P: +919586777575


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.