How to specify a TimeZone when creating a DateTime object in Dart/Flutter?

前端 未结 2 684
广开言路
广开言路 2021-01-12 03:11

Dart?flutter does not appear to allow a timezone (eg: \"Australia/Sydney\" or \"America/Detroit\") to be specified when creating a DateTime object. Either the local timezone

2条回答
  •  孤街浪徒
    2021-01-12 03:47

    The TimeZone package does work. From the issues list on GitHub:

    Sorry this took 6 months but I have a good solution. I think it will be the recommended solution.

    See Flutter's docs on Adding Assets. You should be able to add to your Flutter app's pubspec.yaml:

    flutter:
      assets:
        - packages/timezone/lib/data/2015b.tzf
    

    and load in your app with:

    Future> loadDefaultData() async {
      var byteData = await rootBundle.load('packages/timezone/data/2015b.tzf');
      return byteData.buffer.asUint8List();
    }
    
    // Call the above with something like:
    
    loadDefaultData().then((rawData) {
      initializeDatabase(rawData);
      var zurich = getLocation('Europe/Zurich');
    });
    

提交回复
热议问题