additional information
import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'foos.g.dart';
@Riverpod(keepAlive: true)
class Foos extends _$Foos {
@override
List<String> build() => <String>[];
Future<void> initialize() => Future.sync(
() => state = [for (var i = 0; i < 100; ++i) 'foo'],
);
}
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'foos.dart';
part 'bars.g.dart';
@Riverpod(keepAlive: true, dependencies: [Foos])
class Bars extends _$Bars {
@override
List<String> build() => <String>[];
Future<void> initialize() =>
Future.sync(() => [for (final _ in ref.read(foosProvider)) 'bar']);
}
in Bars
' riverpod annotation dependencies
@Riverpod(keepAlive: true, dependencies: [Foos])
Foos shows the error below
If a provider depends on providers which specify "dependencies", they should themselves specify "dependencies" and include all the scoped providers. dart(provider_dependencies)
can someone help me understand the meaning of this error
and how do I solve it?
environment:
sdk: '>=2.19.4 <3.0.0'
dependencies:
flutter:
sdk: flutter
flutter_riverpod: ^2.3.0
riverpod_annotation: ^2.0.0
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^2.0.1
build_runner: ^2.3.3
custom_lint: ^0.3.2
riverpod_lint: ^1.1.5
riverpod_generator: ^2.0.0
flutter:
uses-material-design: true
include: package:flutter_lints/flutter.yaml
analyzer:
exclude:
- "**/*.g.dart"
plugins:
- custom_lint
You can find the lint's explanation and fixes here.
TL;DR: your Bars
does not depend on Foos
. You can see that there's no ref.watch(foosProvider)
in its build
method. Thus, the lint is triggered as dependencies
shouldn't include Foos
.
Instead, if your initialize
method was meant to be used on build
, you should put its contents in there, specify the dependency through ref.watch
and add it to dependencies
in the annotation, like so:
@Riverpod(keepAlive: true, dependencies: [foos])
class Bards extends _$Bards {
@override
List<String> build() {
final foos = ref.watch(foosProvider);
return [/*... use foos as you do in `initialize`*/];
}
}