srawlins / timezone

Time zone database and time zone aware DateTime object for Dart.
BSD 2-Clause "Simplified" License
102 stars 53 forks source link

Multi Platform Configuration for Flutter #113

Closed ronymesquita closed 2 years ago

ronymesquita commented 3 years ago

Changes

Notes

The depedency of flutter in pubspec.yaml requires some tests to be made with dart command and some with use flutter command. Command File
dart zicfile_test.dart
dart zone_tab_test.dart
flutter datetime_test.dart
flutter datetime_test_no_database.dart

Objective

The main ideia is use Flutter with one configuration. The code of this pull request was tested on Android, iOS, web and Ubuntu (Linux).

srawlins commented 2 years ago

I suspect this code should be a new package, liketimezone_manager or something. It exclusively provides new libraries and new features without relying on internal implementation of the package.

I cannot add a dependency on flutter in this package because it is used outside Flutter.

untp commented 2 years ago

If your main idea is using Flutter with one configuration, you can use this instead:

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  tz.initializeDatabase(Uint8List.sublistView(
      await rootBundle.load('packages/timezone/data/latest_all.tzf')));

  runApp(MyApp());
}

This will work on all platforms including web.

Also updating dependencies' minor or patch versions is not needed, because ^ is used.

ronymesquita commented 2 years ago

Hello. The @untp code is simple and it works. It might be better to just edit the README.md to indicate how to configure Flutter. Is needed to add an asset to pubspec.yaml and put a few lines in the main method. The idea really is use Flutter with one configuration.

saibotma commented 2 years ago

Why is this required at all? For me it works on macOS, Web, iOS and Android when using:

import 'package:timezone/data/latest_10y.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  initializeTimeZones();
  runApp(App());
}
untp commented 2 years ago

initializeTimeZones() method uses embedded data in the dart code. It increases dart snapshot/js size. It is fine if it is used on mobile and desktop. But on web, web page loads slower due to increased dart code size. With rootBundle method, timezone data can be loaded as async, so loading will be faster.

But if you initialize in main(), there should not be huge difference. runApp waits the initialize call, it doesn't matter data loaded with rootBundle or embedded in the dart code.

If your app uses timezone data occasionally and initializes the data outside of main(), it would be better to use rootBundle.

saibotma commented 2 years ago

Thanks for the explanation. Makes a lot of sense. Would be great to have this in the README. Also, additionally, it should tell that you then have to add packages/timezone/data/latest_all.tzf to the assets in pubspec.yml.