-
-
Save ZaidSameer/ca658ff70e0fcdc805edf90f0d66b595 to your computer and use it in GitHub Desktop.
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 'gd_spin_future_builder.dart'; | |
class GdRefreshable<T> extends StatefulWidget { | |
final Future<T> Function() load; | |
final Widget Function(BuildContext, T, void Function() refresh) builder; | |
GdRefreshable(this.load, this.builder); | |
@override | |
State<StatefulWidget> createState() => _GdRefreshableState<T>(); | |
} | |
class _GdRefreshableState<T> extends State<GdRefreshable<T>> { | |
Future<T> loading; | |
@override | |
void initState() { | |
super.initState(); | |
loading = widget.load(); | |
} | |
@override | |
Widget build(BuildContext context) => GdSpinFutureBuilder<T>( | |
loading, | |
(context, data) => widget.builder( | |
context, | |
data, | |
() => setState(() {loading = widget.load();})) | |
); | |
} |
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 '../exception/invalid_token_exception.dart'; | |
typedef Builder<T> = Widget Function(BuildContext, T); | |
class GdSpinFutureBuilder<T> extends StatelessWidget { | |
final Future<T> future; | |
final Builder<T> builder; | |
GdSpinFutureBuilder(this.future, this.builder); | |
@override | |
Widget build(BuildContext _) => FutureBuilder( | |
future: future, | |
builder: (context, snapshot) { | |
if(snapshot.hasError) { | |
if(snapshot.error is InvalidTokenException) { | |
//NOTE: this is a potential source of bugs, see https://github.com/flutter/flutter/issues/16218 for discussion | |
//It may be better to handle this by wrapping the future above (currently line 15) with a .catch that handles InvalidTokenException only | |
print("Your login has expired ${snapshot.error}"); | |
Future<void>.microtask(() { | |
Navigator.pushNamedAndRemoveUntil(_, "/", (r) => false); | |
}); | |
} | |
return Center(child:Text( | |
"Error: ${snapshot.error}", | |
style: TextStyle(color: Colors.red),) | |
); | |
} else if (snapshot.hasData) { | |
return builder(context, snapshot.data); | |
} else { | |
return Center(child: CircularProgressIndicator()); | |
} | |
}, | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment