Search code examples
riverpodriverpod-generatorriverpod-annotationriverpod-lint

how do I fix riverpod_lint error message 'If a provider depends on providers which specify "dependencies"...'


set up

  1. create a new project
  2. add the dependencies visible under additional information
  3. add the files below
  • foos.dart
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'],
      );
}
  • bars.dart
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']);
}

description

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)

question

can someone help me understand the meaning of this error
and how do I solve it?

additional information

  • pubspec.yaml
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
  • analysis_options.yaml
include: package:flutter_lints/flutter.yaml

analyzer:
  exclude:
    - "**/*.g.dart"
  plugins:
    - custom_lint

Solution

  • 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`*/];
      }
    }