A package that creates a value controller that allows a value to be retrieved
❗ In order to start using Value Retriever you must have the Dart SDK installed on your machine.
Add value_retriever
to your pubspec.yaml
:
dependencies:
value_retriever:
Install it:
dart pub get
Value Retreiver works on a similar way that Value Notifier in the way that it creates a controlled value, but unlike Valur Notifier, that allows users to listen for changes, Value Retreiver allows a value to be requested, which will be provided by any handlers that may be managing it.
An use case example. Imagine we are creating a Flutter app that edits text files, a simplified example would be:
class EditorScaffold extends StatefulWidget {
State createState() => _EditorScaffoldState();
}
class _EditorScaffoldState extends State<EditorScaffold> {
late final _textValue = ValueRetriever<String>();
Future<void> _save() async {
context.read<Repoisoty>().save(
await _textValue.retrieve(),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
Toolbar(
icons: [
SaveIcon(
onPressed: _save,
),
],
),
Expanded(child: EditorView(value: _textValue)),
],
),
);
}
}
class EditorView extends StatefulWidget {
EditorView({ super.key, required this.value });
final ValueRetriever<String> value;
State createState() => _EditorViewState();
}
class _EditorViewState extends State<EditorView> {
String value = '';
@override
void initState() {
super.initState();
widget.value.onRetrivement(_retrieveValue);
}
@override
void dispose() {
widget.value.removeHandler(_retrieveValue);
super.dispose();
}
bool _retrieveValue(Completer<String> completer) {
completer.complete(_value);
return true;
}
@override
Widget build(BuildContext context) {
// omitted
}
}