Had*_*uzi 7 dart graphql flutter
我正在使用graphql_flutter包并希望有一个拦截器,对于访问令牌,我希望在我的请求以 401 响应代码响应时获取新令牌。
我认为最好使用全局错误处理程序小部件,您可以使用 yout 查询小部件调用该小部件
这是我的示例错误处理程序
final _appstate = getIt.get<AppState>();
class ExceptionBuilder extends StatelessWidget {
final OperationException exception;
final bool hasData;
final VoidCallback refetch;
const ExceptionBuilder(
{Key key,
@required this.exception,
@required this.hasData,
@required this.refetch})
: super(key: key);
Widget _resolver(BuildContext context) {
if ((exception.linkException is LinkException)) {
return SliverFillRemaining(
hasScrollBody: false,
child: Center(
child: Column(mainAxisSize: MainAxisSize.min, children: [
emptyList(context, icon: Icons.wifi_off, msg: "Network Error"),
FlatButton(
onPressed: refetch,
child: Text(
"retry",
style: TextStyle(color: accentColor),
))
]),
),
);
} else if (exception.graphqlErrors.isNotEmpty) {
List<String> _errors = exception.graphqlErrors[0].message.split(':');
if (_errors[1] == " JWTExpired") {
_appstate.refreshToken();
return SliverFillRemaining(
hasScrollBody: false,
child: Container(
alignment: Alignment.center,
child: masker(context, Loader()),
));
}
return SliverFillRemaining(
hasScrollBody: false,
child: Column(mainAxisSize: MainAxisSize.min, children: [
emptyList(context,
icon: Icons.warning_amber_rounded, msg: "Something went wrong"),
FlatButton(
onPressed: refetch,
child: Text(
"retry",
style: TextStyle(color: accentColor),
))
]),
);
}
return SliverToBoxAdapter(
child: SizedBox.shrink(),
);
}
@override
Widget build(BuildContext context) {
return _resolver(context);
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用 sliver 小部件,因为我在 CustomScrollView 中调用它
这是解析器方法
List<Widget> _resolver(BuildContext context, QueryResult result,
FetchMore fetchMore, Refetch refetch) {
if (result.isLoading && isNull(result.data)) {
return [
SliverFillRemaining(
hasScrollBody: false,
child: Container(
alignment: Alignment.center,
child: masker(context, Loader()),
))
];
}
if (!isNull(result.data)) {
List<PersonMiniSchedule> _schedule = scheduleMiniJsonToList(
result.data['person_max_score_per_schedule'],
);
return [
SliverToBoxAdapter(child: SizedBox(height: 30)),
_schedule.isEmpty
? SliverFillRemaining(
child: Center(
child: emptyList(context,
icon: FontAwesomeIcons.book, msg: "No Schedules Yet.")),
)
: SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
return ScheduleCard(
schedule: _schedule[index],
);
}, childCount: _schedule.length)),
];
}
if (result.hasException) {
return [
ExceptionBuilder(
exception: result.exception,
hasData: isNull(result.data),
refetch: refetch)
];
}
return [
SliverToBoxAdapter(
child: SizedBox.shrink(),
)
];
}
Run Code Online (Sandbox Code Playgroud)
这是查询小部件
Query(
options: QueryOptions(
variables: {'id': _appstate.token.hasuraId},
document: yourQuery()),
builder: (QueryResult result,
{VoidCallback refetch, FetchMore fetchMore}) {
return RefreshIndicator(
onRefresh: () async => refetch(),
child: CustomScrollView(
slivers: [
..._resolver(context, result, fetchMore, refetch),
SliverToBoxAdapter(
child: SizedBox(
height: 200,
)),
],
));
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3541 次 |
| 最近记录: |