Commit 8fec4713d28b5787025b090ab1adfa6619a3cf04

Authored by 吴启风
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,7 +48,7 @@ class _TransitionViewState extends State<TransitionView> {
48 //Log.d('Splash读当前页面:$currentPageName'); 48 //Log.d('Splash读当前页面:$currentPageName');
49 Log.d('Splash读本地, userEntity: $userEntity'); 49 Log.d('Splash读本地, userEntity: $userEntity');
50 int apartInMilliseconds = 0; 50 int apartInMilliseconds = 0;
51 - // 阻塞获取系统配置信息 51 + // 获取系统配置信息
52 AppConfigHelper.getAppConfig(); 52 AppConfigHelper.getAppConfig();
53 // 调一下接口判断一下有效性再往下 53 // 调一下接口判断一下有效性再往下
54 if (userEntity != null && userEntity.token != null) { 54 if (userEntity != null && userEntity.token != null) {
@@ -95,7 +95,9 @@ class _TransitionViewState extends State<TransitionView> { @@ -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 try { 102 try {
101 UserEntity? userEntity = await UserDao.getUserInfo(); 103 UserEntity? userEntity = await UserDao.getUserInfo();
@@ -105,7 +107,10 @@ class _TransitionViewState extends State&lt;TransitionView&gt; { @@ -105,7 +107,10 @@ class _TransitionViewState extends State&lt;TransitionView&gt; {
105 Log.d('Splash重设token, userEntity: $userEntity'); 107 Log.d('Splash重设token, userEntity: $userEntity');
106 UserUtil.saveUser(userEntity); 108 UserUtil.saveUser(userEntity);
107 } 109 }
  110 + //todo 如果为null是否需要清除用户信息?
  111 + completer.complete();
108 } catch (e) { 112 } catch (e) {
  113 + debugPrint('Splash获取用户信息异常:$e');
109 //异常的话弹窗提示重试 114 //异常的话弹窗提示重试
110 showDialog( 115 showDialog(
111 context: context, 116 context: context,
@@ -118,13 +123,20 @@ class _TransitionViewState extends State&lt;TransitionView&gt; { @@ -118,13 +123,20 @@ class _TransitionViewState extends State&lt;TransitionView&gt; {
118 content: const Text('网络异常,请检查网络后重试'), 123 content: const Text('网络异常,请检查网络后重试'),
119 actions: <Widget>[ 124 actions: <Widget>[
120 TextButton( 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 TextButton( 133 TextButton(
125 child: const Text('重试'), 134 child: const Text('重试'),
126 onPressed: () async { 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&lt;TransitionView&gt; { @@ -134,6 +146,8 @@ class _TransitionViewState extends State&lt;TransitionView&gt; {
134 ); 146 );
135 e.logE(); 147 e.logE();
136 } 148 }
  149 + // 等待completer完成,这会阻塞后续代码的执行,直到用户做出选择
  150 + await completer.future;
137 } 151 }
138 152
139 @override 153 @override