Skip to content

Instantly share code, notes, and snippets.

@apgapg
Created April 20, 2020 14:16
Show Gist options
  • Save apgapg/d5342c5b684f4fb2b156b59c954dd7d4 to your computer and use it in GitHub Desktop.
Save apgapg/d5342c5b684f4fb2b156b59c954dd7d4 to your computer and use it in GitHub Desktop.
Wrapper of StreamBuilder for managing error and loading on its own. Fully customisable
import 'package:flutter/material.dart';
import 'package:workozy_app/widgets/helper/stream_error_widget.dart';
import 'package:workozy_app/widgets/helper/stream_loading_widget.dart';
typedef OnData<T> = Widget Function(T data);
typedef OnError = Widget Function(dynamic e);
typedef OnLoading = Widget Function();
class MyStreamBuilder<T> extends StatelessWidget {
MyStreamBuilder({
@required this.stream,
@required this.onData,
this.onErrorRefresh,
this.onError,
this.onLoading,
});
final Stream<T> stream;
final VoidCallback onErrorRefresh;
final OnData<T> onData;
final OnError onError;
final OnLoading onLoading;
@override
Widget build(BuildContext context) {
return StreamBuilder<T>(
builder: (context, snapshot) {
if (snapshot.hasData) {
return onData(snapshot.data);
} else if (snapshot.hasError) {
return onError ?? StreamErrorWidget(snapshot.error, onErrorRefresh);
} else {
return onLoading ?? StreamLoadingWidget();
}
},
stream: stream,
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment