Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Async)NotifierProvider documentation example update #901

Closed
stephane-archer opened this issue Dec 5, 2024 · 2 comments
Closed

(Async)NotifierProvider documentation example update #901

stephane-archer opened this issue Dec 5, 2024 · 2 comments
Assignees
Labels
documentation An opportunity for improving the documentation needs triage

Comments

@stephane-archer
Copy link

Most examples in the documentation present a quite advanced programming style, which makes it unusable for beginners.
In many cases, ChatGPT is the best way to decipher the examples.
The goal should be to make the documentation accessible without prior knowledge.

for example, https://riverpod.dev/docs/providers/notifier_provider is not clear.
With the presence of @freezed with _$Todo extends _$Todos

Here is my simplified example for this page that comes from my codebase and doesn't use advance features

import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:shared_preferences/shared_preferences.dart';

class ConvertionStats {
  final int inputFileSize;
  final int outputFileSize;

  ConvertionStats(this.inputFileSize, this.outputFileSize);
}

class AllTimeConvertionStats extends AsyncNotifier<ConvertionStats> {
  @override
  Future<ConvertionStats> build() async {
    // Load initial state from SharedPreferences.
    final prefs = await SharedPreferences.getInstance();
    final inputFileSize = prefs.getInt('allTimeInputFileSize') ?? 0;
    final outputFileSize = prefs.getInt('allTimeOutputFileSize') ?? 0;
    return ConvertionStats(inputFileSize, outputFileSize);
  }

  Future<void> updateStats(ConvertionStats newStats) async {
    final prefs = await SharedPreferences.getInstance();

    // Persist to SharedPreferences.
    await prefs.setInt('allTimeInputFileSize', newStats.inputFileSize);
    await prefs.setInt('allTimeOutputFileSize', newStats.outputFileSize);

    // Update the state.
    state = AsyncValue.data(newStats);
  }
}

final allTimeConvertionStatsProvider =
    AsyncNotifierProvider<AllTimeConvertionStats, ConvertionStats>(
  () => AllTimeConvertionStats(),
);

How to use it:

final statsAsync = ref.watch(allTimeConvertionStatsProvider);

return statsAsync.when(
  data: (stats) => Text(
    'Input: ${stats.inputFileSize}, Output: ${stats.outputFileSize}',
  ),
  loading: () => CircularProgressIndicator(),
  error: (error, stack) => Text('Error: $error'),
);

How to update the state

final notifier = ref.read(allTimeConvertionStatsProvider.notifier);

notifier.updateStats(ConvertionStats(newInputSize, newOutputSize));

I think this example shows a much more accessible code for AsyncNotifier

@stephane-archer stephane-archer added documentation An opportunity for improving the documentation needs triage labels Dec 5, 2024
@rrousselGit
Copy link
Owner

for example, riverpod.dev/docs/providers/notifier_provider is not clear.
With the presence of @freezed with _$Todo extends _$Todos

Code-generation can be disabled in the sidebar. There's already a variant of all snippets without any of these :)

Screenshot 2024-12-05 at 15 15 47

Code-generation is the recommended approach, and as such the default syntax shown by the docs. But the alternative is already present

@stephane-archer
Copy link
Author

@rrousselGit I would consider code generation an advanced feature, so I would not make it the default, because from my understanding it's only for convenience and requires a good level of knowledge of the provider package. To me, it should default to user-friendly, and have an advanced mode

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation An opportunity for improving the documentation needs triage
Projects
None yet
Development

No branches or pull requests

2 participants