tomo-redx
tomo-redx

Reputation: 101

Riverpod error: Undefined class 'ScopedReader'

Now I use hooks_riverpod package to manage states. Then I wanted to use FutureProvider to fetch data from firestore. I read this official document(API reference). The example on the link said that UI will be like:

Widget build(BuildContext, ScopedReader watch) {
  AsyncValue<Configuration> config = watch(configProvider);

  return config.when(
    loading: () => const CircularProgressIndicator(),
    error: (err, stack) => Text('Error: $err'),
    data: (config) {
      return Text(config.host);
    },
  );
}

I did as it said but I got an error "Undefined class 'ScopedReader'".

my code is below.

Provider definition

final quizListProvider = FutureProvider<List>((ref) async {
  FirebaseFirestore firestore = FirebaseFirestore.instance;
  final quizPack = ref.watch(quizPackProvider).state;
  List<Map> quizList = [];
  for (int i = 0; i < quizPack.length; i++) {
    final content = await firestore.collection('quizes').doc(quizPack[i]).get();
    quizList.add(content.data()!);
  }
  return quizList;
});

UI

class Quiz extends ConsumerWidget {
  @override
  Widget build(BuildContext, ScopedReader watch) {
    AsyncValue<List> quizList = watch(quizListProvider);

    return quizList.when(
      data: (quizList) {
        return Scaffold(
         some widgets are here
        );
      },
      loading: () => const CircularProgressIndicator(),
      error: (err, stack) => Text('Error: $err'),
    );
  }
}

I wonder why it happened. Can you help me plz?

Upvotes: 9

Views: 5011

Answers (1)

danPyk
danPyk

Reputation: 641

Creator of Riverpod changed ScopedReader to WidgetRef, now that's how you should use it:

class Example extends ConsumerWidget {
  @override
  Widget build(BuildContext context, WidgetRef ref) {
    int count = ref.watch(counterProvider);
    ...
  }
}

According to changelog - https://pub.dev/packages/flutter_riverpod/versions/1.0.0-dev.11/changelog#100-dev0

Upvotes: 14

Related Questions