Skip to content
This repository was archived by the owner on Oct 8, 2023. It is now read-only.

Commit 6dd2abc

Browse files
authored
Added Type <T> & WhenError() handler (#1)
Added Type <T> & WhenError() handler
2 parents 96be80a + 50471da commit 6dd2abc

File tree

4 files changed

+62
-38
lines changed

4 files changed

+62
-38
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ build/
99

1010
# Directory created by dartdoc
1111
doc/api/
12+
13+
**/ios/Flutter/flutter_export_environment.sh

example/android/gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
org.gradle.jvmargs=-Xmx1536M
22

3+
android.enableR8=true

example/lib/main.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ class _MyHomePageState extends State<MyHomePage> {
6060
future: widget._api.getRandomCat(),
6161
// this is where the magic happens
6262
rememberFutureResult: true,
63-
whenDone: (dynamic cat) => _showCatWidget(cat),
63+
whenDone: (Cat cat) => _showCatWidget(cat),
64+
whenError: (Object error) => Text(error.toString()),
6465
whenNotDone: Center(child: Text('Loading...'))
6566
),
6667
floatingActionButton: FloatingActionButton(
Lines changed: 57 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,101 @@
11
import 'package:flutter/widgets.dart';
22

3-
class EnhancedFutureBuilder extends StatefulWidget {
3+
class EnhancedFutureBuilder<T> extends StatefulWidget {
44
/// Future to resolve.
5-
final Future future;
5+
final Future<T> future;
6+
67
/// Whether or not the future result should be stored.
78
final bool rememberFutureResult;
9+
810
/// Widget to display when connected to an asynchronous computation.
911
final Widget whenActive;
12+
1013
/// Widget to display when connected to an asynchronous computation and awaiting interaction.
1114
final Widget whenWaiting;
15+
1216
/// Widget to display when not connected to n asynchronous computation.
1317
final Widget whenNone;
18+
1419
/// Widget to display when the asynchronous computation is not done yet.
1520
final Widget whenNotDone;
21+
1622
/// Function to call when the asynchronous computation is done.
17-
final Widget Function(dynamic snapshotData) whenDone;
23+
final Widget Function(T snapshotData) whenDone;
24+
25+
/// Function to call when the asynchronous computation is done with error.
26+
/// If no function is passed, whenNotDone() will be used instead
27+
final Widget Function(Object error) whenError;
28+
1829
/// The data that will be used until a non-null [future] has completed.
1930
///
2031
/// See [FutureBuilder] for more info
21-
final dynamic initialData;
22-
23-
const EnhancedFutureBuilder({
24-
Key key,
25-
@required this.future,
26-
@required this.rememberFutureResult,
27-
@required this.whenDone,
28-
this.whenActive,
29-
this.whenNotDone,
30-
this.whenNone,
31-
this.whenWaiting,
32-
this.initialData
33-
}) :
34-
assert(future != null),
35-
assert(rememberFutureResult != null),
36-
assert(whenDone != null),
37-
super(key: key);
32+
final T initialData;
33+
34+
const EnhancedFutureBuilder(
35+
{Key key,
36+
@required this.future,
37+
@required this.rememberFutureResult,
38+
@required this.whenDone,
39+
@required this.whenNotDone,
40+
this.whenError,
41+
this.whenActive,
42+
this.whenNone,
43+
this.whenWaiting,
44+
this.initialData})
45+
: assert(future != null),
46+
assert(rememberFutureResult != null),
47+
assert(whenDone != null),
48+
assert(whenNotDone != null),
49+
super(key: key);
3850

3951
@override
40-
_EnhancedFutureBuilderState createState() => _EnhancedFutureBuilderState();
52+
_EnhancedFutureBuilderState createState() => _EnhancedFutureBuilderState<T>();
4153
}
4254

43-
class _EnhancedFutureBuilderState extends State<EnhancedFutureBuilder> {
44-
45-
Future _cachedFuture;
55+
class _EnhancedFutureBuilderState<T> extends State<EnhancedFutureBuilder<T>> {
56+
Future<T> _cachedFuture;
4657

47-
@override
58+
@override
4859
void initState() {
4960
super.initState();
5061
_cachedFuture = this.widget.future;
5162
}
5263

5364
@override
5465
Widget build(BuildContext context) {
55-
return FutureBuilder(
56-
future: this.widget.rememberFutureResult ? _cachedFuture : this.widget.future,
66+
return FutureBuilder<T>(
67+
future:
68+
this.widget.rememberFutureResult ? _cachedFuture : this.widget.future,
5769
initialData: this.widget.initialData,
58-
builder: (BuildContext context, snapshot) {
59-
if (this.widget.whenActive != null && snapshot.connectionState == ConnectionState.active) {
70+
builder: (context, snapshot) {
71+
if (this.widget.whenActive != null &&
72+
snapshot.connectionState == ConnectionState.active) {
6073
return this.widget.whenActive;
6174
}
62-
63-
if (this.widget.whenNone != null && snapshot.connectionState == ConnectionState.none) {
75+
76+
if (this.widget.whenNone != null &&
77+
snapshot.connectionState == ConnectionState.none) {
6478
return this.widget.whenNone;
6579
}
66-
67-
if (this.widget.whenWaiting != null && snapshot.connectionState == ConnectionState.waiting) {
80+
81+
if (this.widget.whenWaiting != null &&
82+
snapshot.connectionState == ConnectionState.waiting) {
6883
return this.widget.whenWaiting;
6984
}
7085

7186
if (snapshot.connectionState == ConnectionState.done) {
87+
if (snapshot.hasError) {
88+
if (this.widget.whenError != null) {
89+
return this.widget.whenError(snapshot.error);
90+
} else {
91+
return this.widget.whenNotDone;
92+
}
93+
}
7294
return this.widget.whenDone(snapshot.data);
7395
}
7496

75-
if (this.widget.whenNotDone != null && snapshot.connectionState != ConnectionState.done) {
76-
return this.widget.whenNotDone;
77-
}
97+
return this.widget.whenNotDone;
7898
},
7999
);
80100
}
81-
}
101+
}

0 commit comments

Comments
 (0)