Skip to content

Commit

Permalink
Update random number example from StateNotifier to NotifierProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
nestorsgarzonc authored Oct 21, 2024
1 parent 0da12bc commit f385daf
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions examples/random_number/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'dart:math';
1import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
Expand All @@ -9,18 +9,17 @@ void main() {

// State notifier for generating a random number exposed by a state notifier
// provider
class RandomNumberGenerator extends StateNotifier<int> {
RandomNumberGenerator() : super(Random().nextInt(9999));

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 = StateNotifierProvider(
(ref) => RandomNumberGenerator(),
);
final randomNumberProvider = NotifierProvider(RandomNumberGenerator.new);

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

0 comments on commit f385daf

Please sign in to comment.