Commit 8fec4713d28b5787025b090ab1adfa6619a3cf04
1 parent
c4458dc6
feat:启动请求失败阻塞优化
Showing
1 changed file
with
20 additions
and
6 deletions
lib/app/splash_page.dart
... | ... | @@ -48,7 +48,7 @@ class _TransitionViewState extends State<TransitionView> { |
48 | 48 | //Log.d('Splash读当前页面:$currentPageName'); |
49 | 49 | Log.d('Splash读本地, userEntity: $userEntity'); |
50 | 50 | int apartInMilliseconds = 0; |
51 | - // 阻塞获取系统配置信息 | |
51 | + // 获取系统配置信息 | |
52 | 52 | AppConfigHelper.getAppConfig(); |
53 | 53 | // 调一下接口判断一下有效性再往下 |
54 | 54 | if (userEntity != null && userEntity.token != null) { |
... | ... | @@ -95,7 +95,9 @@ class _TransitionViewState extends State<TransitionView> { |
95 | 95 | }); |
96 | 96 | } |
97 | 97 | |
98 | - Future<void> fetchNecessaryData(String userToken) async { | |
98 | + Future<void> fetchNecessaryData(String userToken, | |
99 | + {Completer<void>? completer}) async { | |
100 | + completer ??= Completer<void>(); | |
99 | 101 | // 获取用户信息 |
100 | 102 | try { |
101 | 103 | UserEntity? userEntity = await UserDao.getUserInfo(); |
... | ... | @@ -105,7 +107,10 @@ class _TransitionViewState extends State<TransitionView> { |
105 | 107 | Log.d('Splash重设token, userEntity: $userEntity'); |
106 | 108 | UserUtil.saveUser(userEntity); |
107 | 109 | } |
110 | + //todo 如果为null是否需要清除用户信息? | |
111 | + completer.complete(); | |
108 | 112 | } catch (e) { |
113 | + debugPrint('Splash获取用户信息异常:$e'); | |
109 | 114 | //异常的话弹窗提示重试 |
110 | 115 | showDialog( |
111 | 116 | context: context, |
... | ... | @@ -118,13 +123,20 @@ class _TransitionViewState extends State<TransitionView> { |
118 | 123 | content: const Text('网络异常,请检查网络后重试'), |
119 | 124 | actions: <Widget>[ |
120 | 125 | TextButton( |
121 | - child: const Text('退出'), | |
122 | - onPressed: () => AppConfigHelper.exitApp() | |
123 | - ), | |
126 | + child: const Text('退出'), | |
127 | + onPressed: () => { | |
128 | + // 关闭对话框并重试 | |
129 | + Navigator.of(context).pop(), | |
130 | + AppConfigHelper.exitApp(), | |
131 | + completer?.complete(), | |
132 | + }), | |
124 | 133 | TextButton( |
125 | 134 | child: const Text('重试'), |
126 | 135 | onPressed: () async { |
127 | - fetchNecessaryData(userToken); | |
136 | + // 关闭对话框并重试 | |
137 | + Navigator.of(context).pop(); | |
138 | + await fetchNecessaryData(userToken, completer: completer); | |
139 | + completer?.complete(); | |
128 | 140 | }, |
129 | 141 | ), |
130 | 142 | ], |
... | ... | @@ -134,6 +146,8 @@ class _TransitionViewState extends State<TransitionView> { |
134 | 146 | ); |
135 | 147 | e.logE(); |
136 | 148 | } |
149 | + // 等待completer完成,这会阻塞后续代码的执行,直到用户做出选择 | |
150 | + await completer.future; | |
137 | 151 | } |
138 | 152 | |
139 | 153 | @override | ... | ... |