search method
Search is the main search state manager method that calls future. and emits resulted AsyncSnapshot to ValueNotifier.
Before calling the future, value.snapshot
's state will be updated
with ConnectionState.waiting.
Implementation
Future<void> search(String input) async {
input = input.trim();
if (future == null) return;
if (value.lastInput == input) return;
value.lastInput = input;
if (input.isEmpty) return onEmptyData?.call(value.snapshot);
final prev = value.previousSnapshot;
if (prev == null || prev.connectionState != value.snapshot.connectionState) {
value.snapshot = value.snapshot.inState(ConnectionState.waiting);
notifyListeners();
onLoad?.call(value.snapshot);
}
final Object callbackIdentity = Object();
value.activeCallbackIdentity = callbackIdentity;
future!.call(input).then<void>((List<T> data) {
value.snapshot = AsyncSnapshot<List<T>>.withData(
ConnectionState.done,
data,
);
notifyListeners();
if (data.isEmpty) return onEmptyData?.call(value.snapshot);
onData?.call(value.snapshot);
}, onError: (Object error, StackTrace stackTrace) {
value.snapshot = AsyncSnapshot<List<T>>.withError(
ConnectionState.done,
error,
stackTrace,
);
notifyListeners();
onError?.call(value.snapshot);
assert(() {
if (FutureBuilder.debugRethrowError) {
Future<Object>.error(error, stackTrace);
}
return true;
}());
});
// Keep updating the previous value.
value.previousSnapshot = value.snapshot;
}