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

add simple example on how to listen other providers #3871

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 125 additions & 0 deletions website/docs/essentials/listen_other_providers.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
---
title: listen other providers
version: 1
---

import { Link } from "/src/components/Link";
import { AutoSnippet, When } from "/src/components/CodeSnippet";

```dart
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

void main() {
runApp(const ProviderScope(child: MyApp()));
}

final minutesProvider = NotifierProvider<MinutesNotifer, int>(() {
return MinutesNotifer();
});

final secondProvider = NotifierProvider<SecondsNotifer, int>(() {
return SecondsNotifer();
});
Comment on lines +17 to +23
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix typo in class names

There's a typo in the class names: MinutesNotifer and SecondsNotifer should be MinutesNotifier and SecondsNotifier (missing 'i' in "Notifier").

-final minutesProvider = NotifierProvider<MinutesNotifer, int>(() {
-  return MinutesNotifer();
+final minutesProvider = NotifierProvider<MinutesNotifier, int>(() {
+  return MinutesNotifier();
});

-final secondProvider = NotifierProvider<SecondsNotifer, int>(() {
-  return SecondsNotifer();
+final secondProvider = NotifierProvider<SecondsNotifier, int>(() {
+  return SecondsNotifier();
});
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
final minutesProvider = NotifierProvider<MinutesNotifer, int>(() {
return MinutesNotifer();
});
final secondProvider = NotifierProvider<SecondsNotifer, int>(() {
return SecondsNotifer();
});
final minutesProvider = NotifierProvider<MinutesNotifier, int>(() {
return MinutesNotifier();
});
final secondProvider = NotifierProvider<SecondsNotifier, int>(() {
return SecondsNotifier();
});


class MinutesNotifer extends Notifier<int> {
@override
int build() {
ref.listen(secondProvider, (prev, next) {
if (prev == 59 && next == 0) {
increment();
}
});
return 0;
}

void increment() {
var nextState = state;
nextState = nextState + 1;
if (nextState == 60) {
nextState = 0;
}
state = nextState;
}
}

class SecondsNotifer extends Notifier<int> {
@override
int build() {
return 0;
}

void increment() {
var nextState = state;
nextState = nextState + 1;
if (nextState == 60) {
nextState = 0;
}
state = nextState;
}

int value() {
return state;
}
}
Comment on lines +46 to +64
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Remove unnecessary value() method and simplify increment logic

  1. The value() method is redundant as the state can be accessed directly through the provider.
  2. The increment logic can be simplified similar to the minutes notifier.
 class SecondsNotifer extends Notifier<int> {
   @override
   int build() {
     return 0;
   }

   void increment() {
-    var nextState = state;
-    nextState = nextState + 1;
-    if (nextState == 60) {
-      nextState = 0;
-    }
-    state = nextState;
+    state = (state + 1) % 60;
   }

-  int value() {
-    return state;
-  }
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
class SecondsNotifer extends Notifier<int> {
@override
int build() {
return 0;
}
void increment() {
var nextState = state;
nextState = nextState + 1;
if (nextState == 60) {
nextState = 0;
}
state = nextState;
}
int value() {
return state;
}
}
class SecondsNotifer extends Notifier<int> {
@override
int build() {
return 0;
}
void increment() {
state = (state + 1) % 60;
}
}


class MyApp extends StatelessWidget {
const MyApp({super.key});

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(),
);
}
}

class MyHomePage extends ConsumerWidget {
const MyHomePage({super.key});

@override
Widget build(BuildContext context, WidgetRef ref) {
var secondsNotifer = ref.watch(secondProvider.notifier);
var nbMinutes = ref.watch(minutesProvider);
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: const Text('The manual Clock'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'press the button to add seconds to the clock:',
),
Text(
'${ref.watch(secondProvider)}',
style: Theme.of(context).textTheme.headlineMedium,
),
if (nbMinutes != 0)
const Text(
'number of minutes:',
),
if (nbMinutes != 0)
Text(
'$nbMinutes',
style: Theme.of(context).textTheme.headlineMedium,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: secondsNotifer.increment,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}
```
Loading