Last active
December 30, 2022 17:53
-
-
Save mh61503891/2c9f9aee20ce1e36f0800dfccf070651 to your computer and use it in GitHub Desktop.
SnackBar with Riverpod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:flutter_riverpod/flutter_riverpod.dart'; | |
// ... | |
ref.listen( | |
snackBarProvider, | |
(previous, next) { | |
if (mounted && next != null) { | |
ScaffoldMessenger.of(context).showSnackBar(next); | |
} | |
}, | |
onError: (error, stackTrace) { | |
// TODO implement | |
}, | |
); | |
// ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:flutter_riverpod/flutter_riverpod.dart'; | |
final snackBarProvider = | |
StateNotifierProvider<SnackBarNotifier, SnackBar?>((ref) { | |
return SnackBarNotifier(); | |
}); | |
class SnackBarNotifier extends StateNotifier<SnackBar?> { | |
SnackBarNotifier() : super(null); | |
setSnackBar(SnackBar snackBar) { | |
state = snackBar; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:flutter_riverpod/flutter_riverpod.dart'; | |
// ... | |
ref.read(snackBarProvider.notifier).setSnackBar( | |
SnackBar(content: Text('Hello, world!')), | |
); | |
// ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment