Created
November 14, 2023 06:04
-
-
Save dickermoshe/2a617855283a85fb6c1e7c639be16269 to your computer and use it in GitHub Desktop.
An extension for `AsyncSnapshot` with the `when` method from Riverpod's `AsyncValue` : Simplily use of `FutureBuilder`
This file contains hidden or 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'; | |
extension When<T> on AsyncSnapshot<T> { | |
R when<R>({ | |
required R Function(T data) data, | |
required R Function(Object error, StackTrace stackTrace) error, | |
required R Function() loading, | |
}) { | |
switch (connectionState) { | |
case ConnectionState.none: | |
return loading(); | |
case ConnectionState.waiting: | |
return loading(); | |
case ConnectionState.active: | |
return loading(); | |
case ConnectionState.done: | |
if (hasError) { | |
return error(error, stackTrace!); | |
} else { | |
return data(this.data as T); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment