Created
April 20, 2020 14:16
-
-
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
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: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