rrousselGit / riverpod

A reactive caching and data-binding framework. Riverpod makes working with asynchronous code a breeze.
https://riverpod.dev
MIT License
5.95k stars 918 forks source link

Add more documentation about code generation #1814

Closed AbdulHaseeeb closed 1 year ago

AbdulHaseeeb commented 1 year ago

Thanks for the amazing package. Code generation will make riverpod much more easier. As a beginner its hard for me to sort out how to work with riverpod code generation, without some examples and documentation, for instance, I have a simple counterNotifier

import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'providers.g.dart';

class CounterNotifier extends StateNotifier<int> {
  CounterNotifier({required counter}) : super(counter);

  increment() {
    state = state + 1;
  }
}

//using providers
final counterNotifierProvider =
    StateNotifierProvider<CounterNotifier, int>((ref) {
  return CounterNotifier(counter: 0);
});

//using generators
@riverpod
numberNotifier(NumberNotifierRef ref) {
  return CounterNotifier(counter: 0);
}

whats the use of NumberNotifierRef ? Did riverpod select StateNotifierProvider automatically for my Counter Notifier in numberNotifier function ? as it says in the documentation ? If yes, is there any way to verify this ?

In my ui I cannot access increment method with numberNotifierProvider.

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:isardb/providers.dart';

class HomeScreen extends ConsumerStatefulWidget {
  const HomeScreen({super.key});

  @override
  ConsumerState<ConsumerStatefulWidget> createState() => _HomeScreenState();
}

class _HomeScreenState extends ConsumerState<HomeScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text(
          ref.watch(counterNotifierProvider).toString(),
          // ref.watch(numberNotifierProvider)
          style: const TextStyle(fontSize: 54),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          //can access notifier like this
          ref.read(counterNotifierProvider.notifier).increment();

          //ERROR : no notifier to access increment to update state
          //ref.read(numberNotifierProvider.notifer).increment();
        },
        child: const Icon(Icons.add),
      ),
    );
  }
}

If it selected StateNotifierProvider automatically it should work just like that.

rrousselGit commented 1 year ago

Hello!

whats the use of NumberNotifierRef ?

This is covered by the "reading" doc: https://docs-v2.riverpod.dev/docs/concepts/reading

Did riverpod select StateNotifierProvider automatically for my Counter Notifier in numberNotifier function ? as it says in the documentation ? If yes, is there any way to verify this ?

No. Cf #1788 and #1663

rrousselGit commented 1 year ago

You typically don't write "StateNotifiers" with the code-generator.

Instead do:

@riverpod
class CounterNotifier extends _$CounterNotifier {
  @override
  int build() => 0;

  increment() {
    state = state + 1;
  }
}
hendrakurniawan88 commented 1 year ago

@riverpod class CounterNotifier extends _$CounterNotifier { @override int build() => 0;

increment() { state = state + 1; } }

hi @rrousselGit , can you give more explanation about the build() part