Articles

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

by Manish Patel Flutter App Development Company
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 cost-saving and willingness to compromise without any overheads.

About Manish Patel Advanced     Flutter App Development Company

82 connections, 2 recommendations, 281 honor points.
Joined APSense since, February 16th, 2018, From Ahmedabad, India.

Created on Oct 11th 2019 08:34. Viewed 29 times.

Comments

No comment, be the first to comment.
Please sign in before you comment.