avoid-ref-inside-state-dispose
- Group
- Correctness
- Path
lint/correctness/avoid-ref-inside-state-dispose- Default
- Off, opt in
- Origin
- Falcon
- Domain
- riverpod
- Rule
- 124 of 512
riverpod
Rejects Riverpod widget references used during State.dispose.
Invalid
example.dartdart
import 'package:flutter_riverpod/flutter_riverpod.dart';
abstract class First extends ConsumerState<Object> {
void dispose() {
ref.read(Object());
this.ref.read(Object());
final cached = ref;
use(ref);
(() { ref.read(Object()); })();
super.dispose();
}
}
void use(WidgetRef value) {}
mixin DisposeMixin<W extends Object> on ConsumerState<W> {
@override
void dispose() {
ref.read(Object());
this.ref.read(Object());
super.dispose();
}
}Do not use a widget ref during dispose; cache the required resource before disposal.
3
void dispose() {4
ref.read(Object());Do not use a widget ref during dispose; cache the required resource before disposal.
4
ref.read(Object());5
this.ref.read(Object());Do not use a widget ref during dispose; cache the required resource before disposal.
5
this.ref.read(Object());6
final cached = ref;Do not use a widget ref during dispose; cache the required resource before disposal.
6
final cached = ref;7
use(ref);Do not use a widget ref during dispose; cache the required resource before disposal.
7
use(ref);8
(() { ref.read(Object()); })();Do not use a widget ref during dispose; cache the required resource before disposal.
16
void dispose() {17
ref.read(Object());Do not use a widget ref during dispose; cache the required resource before disposal.
17
ref.read(Object());18
this.ref.read(Object());Valid
example.dartdart
import 'package:flutter_riverpod/flutter_riverpod.dart';
abstract class Good extends ConsumerState<Object> {
void initState() { ref.read(Object()); }
void build() { this.ref.read(Object()); }
void dispose() {
final ref = FakeRef();
ref.read(Object());
super.dispose();
}
static void disposeOther(WidgetRef ref) { ref.read(Object()); }
}
class FakeRef { void read(Object value) {} }
class Other { WidgetRef? ref; void dispose() { ref?.read(Object()); } }
void dispose(WidgetRef ref) { ref.read(Object()); }
mixin BuildMixin<W extends Object> on ConsumerState<W> {
void build() { ref.read(Object()); }
@override
void dispose() {
final ref = FakeRef();
ref.read(Object());
super.dispose();
}
}
mixin OtherMixin {
WidgetRef get ref;
void dispose() { ref.read(Object()); }
}Configure avoid-ref-inside-state-dispose
Set its severity in falcon.json. See the configuration guide.
falcon.jsonjson
{
"linter": {
"rules": {
"correctness": {
"avoid-ref-inside-state-dispose": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
example.dartdart
// falcon-ignore lint/correctness/avoid-ref-inside-state-dispose: <reason>More correctness rules
- avoid-ref-read-inside-buildrecommendedriverpod
Flags one-time Riverpod reads of provider state during widget build.
- avoid-ref-watch-outside-buildrecommendedriverpod
Flags Riverpod WidgetRef.watch subscriptions outside synchronous widget build bodies.
- avoid-relative-lib-imports
avoid_relative_lib_importsrecommendedDisallow relative imports that reach into a
lib/directory. - avoid-returning-widgets
avoid-returning-widgetsflutterDisallow functions and methods that return a
Widget. - avoid-type-to-string
avoid_type_to_stringrecommendedAvoid
Type.toString()in production code, where type names may be minified. - avoid-uncaught-future-errorsrecommended
Flags local futures first awaited after another await inside a try/catch.
- avoid-unconditional-break
Reports unconditional break, continue, return or throw in a for-loop body.
- avoid-undisposed-instancesrecommendedflutter
Warns when a disposable constructor result is discarded.