ForbidDateTimeNow class
Lint rule that forbids using DateTime.now() in production code.
This rule enforces the use of the custom Clock interface from
libraries/time/interfaces/clock.dart instead of direct DateTime.now() calls.
This is essential for:
- Deterministic testing: Tests can control the current time
- Widget testing: Time-dependent widgets can be tested reliably
- Mocking: Time can be easily mocked in unit and integration tests
Exception: DateTime.now() is allowed in system_clock_impl.dart where the
Clock implementation is defined.
Example of code that triggers this rule:
final currentTime = DateTime.now(); // LINT: Use clock.now() instead
Example of correct usage:
import 'libraries/time/interfaces/clock.dart';
// Inject clock via constructor or use dependency injection
final Clock clock;
final currentTime = clock.now(); // OK - testable
- Inheritance
-
- Object
- BaseLintRule
- ForbidDateTimeNow
Constructors
Properties
- analyzer → BaseAnalyzer
-
no setteroverride
- code → LintCode
-
The
LintCodethat thisLintRulemay emit.finalinherited - enabledByDefault → bool
-
Whether the lint rule is on or off by default in an empty analysis_options.yaml
no setterinherited
-
filesToAnalyze
→ List<
String> -
A list of glob patterns matching the files that run cares about.
no setterinherited
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
getFixes(
) → List< Fix> -
Obtains the list of
Fixassociated with thisLintRule.inherited -
isEnabled(
CustomLintConfigs configs) → bool -
Checks whether this lint rule is enabled in a configuration file.
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
run(
CustomLintResolver resolver, ErrorReporter reporter, CustomLintContext context) → void -
Emits lints for a given file.
inherited
-
startUp(
CustomLintResolver resolver, CustomLintContext context) → Future< void> -
Emits lints for a given file.
inherited
-
testAnalyzeAndRun(
File file) → Future< List< AnalysisError> > -
Analyze a Dart file and runs this assist in test mode.
inherited
-
testRun(
ResolvedUnitResult result, {Pubspec? pubspec}) → Future< List< AnalysisError> > -
Runs this assist in test mode.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited