performance fixes
This commit is contained in:
@@ -25,29 +25,31 @@ class BetterStreamBuilder<T> extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _BetterStreamBuilderState<T> extends State<BetterStreamBuilder<T>> {
|
||||
Widget? _child;
|
||||
T? _lastEvent;
|
||||
StreamSubscription? _subscription;
|
||||
Object? _lastError;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => _child ?? const Offstage();
|
||||
Widget build(BuildContext context) {
|
||||
if (_lastError != null) {
|
||||
return widget.errorBuilder!(context, _lastError!);
|
||||
}
|
||||
|
||||
if (_lastEvent == null) {
|
||||
return widget.loadingBuilder?.call(context) ?? const Offstage();
|
||||
}
|
||||
return widget.builder(context, _lastEvent ?? widget.initialData);
|
||||
}
|
||||
|
||||
bool _firstTime = true;
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
if (_firstTime) {
|
||||
if (widget.initialData == null && widget.loadingBuilder != null) {
|
||||
_child = widget.loadingBuilder!(context);
|
||||
} else {
|
||||
_onEvent(widget.initialData);
|
||||
}
|
||||
_lastEvent = widget.initialData;
|
||||
_subscription = widget.stream?.listen(
|
||||
_onEvent,
|
||||
onError: _onError,
|
||||
);
|
||||
|
||||
_firstTime = false;
|
||||
}
|
||||
super.didChangeDependencies();
|
||||
@@ -55,12 +57,6 @@ class _BetterStreamBuilderState<T> extends State<BetterStreamBuilder<T>> {
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant BetterStreamBuilder<T> oldWidget) {
|
||||
if (_lastError != null && oldWidget.errorBuilder != widget.errorBuilder) {
|
||||
_onError(_lastError);
|
||||
} else if (oldWidget.builder != widget.builder) {
|
||||
_onEvent(_lastEvent);
|
||||
}
|
||||
|
||||
if (oldWidget.stream != widget.stream) {
|
||||
_subscription?.cancel();
|
||||
_subscription = widget.stream?.listen(
|
||||
@@ -79,21 +75,22 @@ class _BetterStreamBuilderState<T> extends State<BetterStreamBuilder<T>> {
|
||||
|
||||
void _onError(error) {
|
||||
if (widget.errorBuilder != null && error != _lastError) {
|
||||
setState(() {
|
||||
_child = widget.errorBuilder!(context, error);
|
||||
});
|
||||
if (mounted) {
|
||||
setState(() {});
|
||||
}
|
||||
_lastError = error;
|
||||
}
|
||||
}
|
||||
|
||||
void _onEvent(event) {
|
||||
void _onEvent(T event) {
|
||||
_lastError = null;
|
||||
if (widget.comparator != null
|
||||
final isEqual = widget.comparator != null
|
||||
? widget.comparator!(_lastEvent, event)
|
||||
: event != _lastEvent) {
|
||||
setState(() {
|
||||
_child = widget.builder(context, event);
|
||||
});
|
||||
: event == _lastEvent;
|
||||
if (!isEqual) {
|
||||
if (mounted) {
|
||||
setState(() {});
|
||||
}
|
||||
_lastEvent = event;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -346,36 +346,40 @@ class StreamChannelState extends State<StreamChannel> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
Widget child = FutureBuilder<List<bool>>(
|
||||
future: Future.wait(_futures),
|
||||
initialData: [
|
||||
channel.state != null,
|
||||
if (initialMessageId != null) false,
|
||||
],
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.hasError) {
|
||||
var message = snapshot.error.toString();
|
||||
if (snapshot.error is DioError) {
|
||||
final dioError = snapshot.error as DioError?;
|
||||
if (dioError?.type == DioErrorType.response) {
|
||||
message = dioError!.message;
|
||||
} else {
|
||||
message = 'Check your connection and retry';
|
||||
var child = widget.child;
|
||||
if (widget.showLoading &&
|
||||
(initialMessageId != null || channel.state == null)) {
|
||||
child = FutureBuilder<List<bool>>(
|
||||
future: Future.wait(_futures),
|
||||
initialData: [
|
||||
channel.state != null,
|
||||
if (initialMessageId != null) false,
|
||||
],
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.hasError) {
|
||||
var message = snapshot.error.toString();
|
||||
if (snapshot.error is DioError) {
|
||||
final dioError = snapshot.error as DioError?;
|
||||
if (dioError?.type == DioErrorType.response) {
|
||||
message = dioError!.message;
|
||||
} else {
|
||||
message = 'Check your connection and retry';
|
||||
}
|
||||
}
|
||||
return Center(child: Text(message));
|
||||
}
|
||||
return Center(child: Text(message));
|
||||
}
|
||||
final initialized = snapshot.data![0];
|
||||
// ignore: avoid_bool_literals_in_conditional_expressions
|
||||
final dataLoaded = initialMessageId == null ? true : snapshot.data![1];
|
||||
if (widget.showLoading && (!initialized || !dataLoaded)) {
|
||||
return const Center(
|
||||
child: CircularProgressIndicator(),
|
||||
);
|
||||
}
|
||||
return widget.child;
|
||||
},
|
||||
);
|
||||
final initialized = snapshot.data![0];
|
||||
final dataLoaded = initialMessageId == null || snapshot.data![1];
|
||||
if (!initialized || !dataLoaded) {
|
||||
return const Center(
|
||||
child: CircularProgressIndicator(),
|
||||
);
|
||||
}
|
||||
return widget.child;
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
if (initialMessageId != null) {
|
||||
child = Material(child: child);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user