Skip to content

Commit

Permalink
Refactor random number example to use NotifierProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
nestorsgarzonc committed Oct 21, 2024
1 parent 3ef49b5 commit cead49e
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions examples/random_number/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@ void main() {
runApp(const ProviderScope(child: RandomNumberApp()));
}

// State notifier for generating a random number exposed by a state notifier
// Notifier for generating a random number exposed by a state notifier
// provider
class RandomNumberGenerator extends Notifier<int> {
class RandomNumberGenerator extends Notifier<int> {
@override
int build() => Random().nextInt(9999);

void generate() {
state = Random().nextInt(9999);
}
}

// State notifier provider holding the state
final randomNumberProvider = NotifierProvider(RandomNumberGenerator.new);
// Notifier provider holding the state
final randomNumberProvider =
NotifierProvider<RandomNumberGenerator, int>(RandomNumberGenerator.new);

class RandomNumberApp extends StatelessWidget {
const RandomNumberApp({super.key});
Expand Down

0 comments on commit cead49e

Please sign in to comment.