導航到下一頁后,我的啟影片面出現問題
class SplashScreen extends StatelessWidget {
final backgroundColor = 0xffF6F6F6;
@override
Widget build(BuildContext context) {
Future.delayed(const Duration(seconds: 3), () {
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (context) => Wrapper(),
),
);
});
return ScreenUtilInit(
builder: () => MaterialApp(...),
);
}
}
簡單的 'Navigator.of(context).push' 作業正常,沒有顯示但使用 pushReplacement 下一個錯誤顯示:
D/NetworkSecurityConfig( 8102): No Network Security Config specified, using platform default
W/System ( 8102): Ignoring header X-Firebase-Locale because its value was null.
W/DynamiteModule( 8102): Local module descriptor class for providerinstaller not found.
I/DynamiteModule( 8102): Considering local module providerinstaller:0 and remote module providerinstaller:0
W/ProviderInstaller( 8102): Failed to load providerinstaller module: No acceptable module found. Local version is 0 and remote version is 0.
I/com.my_app( 8102): The ClassLoaderContext is a special shared library.
I/chatty ( 8102): uid=10088(com.my_app) AsyncTask #2 identical 1 line
I/com.my_app( 8102): The ClassLoaderContext is a special shared library.
V/NativeCrypto( 8102): Registering com/google/android/gms/org/conscrypt/NativeCrypto's 294 native methods...
W/com.my_app( 8102): Accessing hidden method Ljava/security/spec/ECParameterSpec;->getCurveName()Ljava/lang/String; (light greylist, reflection)
I/ProviderInstaller( 8102): Installed default security provider GmsCore_OpenSSL
W/com.my_app( 8102): Accessing hidden method Ljava/security/spec/ECParameterSpec;->setCurveName(Ljava/lang/String;)V (light greylist, reflection)
D/FirebaseAuth( 8102): Notifying id token listeners about user ( sXisKRx15aeLiT7rGR8XS4yGGNJ2 ).
E/flutter ( 8102): [ERROR:flutter/shell/common/shell.cc(93)] Dart Unhandled Exception: Looking up a deactivated widget's ancestor is unsafe.
E/flutter ( 8102): At this point the state of the widget's element tree is no longer stable.
E/flutter ( 8102): To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor by calling dependOnInheritedWidgetOfExactType() in the widget's didChangeDependencies() method., stack trace: #0 Element._debugCheckStateIsActiveForAncestorLookup.<anonymous closure> (package:flutter/src/widgets/framework.dart:4032:9)
E/flutter ( 8102): #1 Element._debugCheckStateIsActiveForAncestorLookup (package:flutter/src/widgets/framework.dart:4046:6)
E/flutter ( 8102): #2 Element.findAncestorStateOfType (package:flutter/src/widgets/framework.dart:4093:12)
E/flutter ( 8102): #3 Navigator.of (package:flutter/src/widgets/navigator.dart:2736:40)
E/flutter ( 8102): #4 SplashScreen.build.<anonymous closure> (package:first_project_test/screens/home/splash.dart:13:17)
E/flutter ( 8102): #5 new Future.delayed.<anonymous closure> (dart:async/future.dart:315:39)
E/flutter ( 8102): #6 Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:18:15)
E/flutter ( 8102): #7 _Timer._runTimers (dart:isolate-patch/timer_impl.dart:395:19)
E/flutter ( 8102): #8 _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:426:5)
E/flutter ( 8102): #9 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)
它僅在我使用“Run 'main.dart'”重新渲染頁面時顯示,我用 ScreenUtilInit 包裝了 Material 應用程式,因為
我還在我的主檔案中使用 InitializerWidget 類運行 SplashScreen,如果您需要有關它的更多資訊,請告訴我?
uj5u.com熱心網友回復:
我們不應該build直接呼叫方法內部的函式。當一個邏輯只需要在創建 Widget 的狀態時實作一次時,就
使用initState h。
- 更改
Stateless為Stateful
class SplashScreen extends StatefulWidget {
const SplashScreen({Key? key}) : super(key: key);
@override
_SplashScreenState createState() => _SplashScreenState();
}
class _SplashScreenState extends State<SplashScreen> {
@override
Widget build(BuildContext context) {
return SomeWidget();
}
}
- 現在將所需的邏輯放在
initState方法中。
class _SplashScreenState extends State<SplashScreen> {
@override
initState() {
super.initState();
Future.delayed(const Duration(seconds: 3), () {
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (context) => Wrapper(),
),
);
});
}
// other methods
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/342554.html
上一篇:Dart-以Set為鍵的映射
