Last active
July 9, 2020 12:13
-
-
Save magicleon94/8ae4f73d42fe6c60c5f1f5c08fc20b39 to your computer and use it in GitHub Desktop.
A simple helper to map states of a snapshot coming from a StreamBuilder
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'; | |
typedef SnapshotBuilder<T> = Widget Function(AsyncSnapshot<T>); | |
class SnapshotHelper<T> { | |
final AsyncSnapshot<T> snapshot; | |
SnapshotHelper._(this.snapshot); | |
factory SnapshotHelper.of(AsyncSnapshot snapshot) => | |
SnapshotHelper._(snapshot); | |
Widget getWidget( | |
{SnapshotBuilder<T> onData, | |
SnapshotBuilder<T> onError, | |
SnapshotBuilder<T> onLoading}) { | |
if (snapshot.hasError) { | |
return onError(snapshot); | |
} else if (snapshot.connectionState == ConnectionState.waiting) { | |
return onLoading(snapshot); | |
} else if (snapshot.connectionState == ConnectionState.active && | |
!snapshot.hasData) { | |
return onLoading(snapshot); | |
} else if (snapshot.hasData && !snapshot.hasError) { | |
return onData(snapshot); | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment