time_machine
time_machine copied to clipboard
Update Installation Guide and Example.
I was really struggling to get this project up and running since the Example had no indication that the rootBundle argument must be passed to TimeMachine.initialize
nor does the actual method have it as a required parameter.
Secondly, once I figured that out I struggled some more with an additional asset loading error which after much debugging I found mentioned in the API Reference that 2 assets need to be listed in pubsec.yaml
, there's no mention of this in the Installation Guide.
I'm going to add the 3 main errors I was dealing with down below so that others that have similar errors can finally figure out how this library needs to be set up (until the documentation is updated), If I have the time I'll try to create a PR myself.
- Unhandled Exception: Exception: Pass in the rootBundle from 'package:flutter/services.dart';
- Unhandled Exception: NoSuchMethodError: The method 'getJson' was called on null.
- Unhandled Exception: Unable to load asset: packages/time_machine/data/tzdb/tzdb.bin
For easy copy-pasting here are the solutions:
Add to pubsec.yaml
:
assets:
- packages/time_machine/data/cultures/cultures.bin
- packages/time_machine/data/tzdb/tzdb.bin
How to Initialize TimeMachine:
await TimeMachine.initialize({
'rootBundle': rootBundle,
});
I see on the pub.dev page for time_machine, there is an example to initialize. But, it is incorrect:
import 'package:flutter/services.dart';
// TimeMachine discovers your TimeZone heuristically (it's actually pretty fast).
await TimeMachine.initialize({rootBundle: rootBundle});
It should instead have the rootBundle
key quoted as a string:
import 'package:flutter/services.dart';
// TimeMachine discovers your TimeZone heuristically (it's actually pretty fast).
await TimeMachine.initialize({'rootBundle': rootBundle});
If you do the initialization in the main method, make sure you call this before:
WidgetsFlutterBinding.ensureInitialized();