I'm facing problem in my splash screen after navigating to the next page
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(...),
);
}
}
Simple 'Navigator.of(context).push' works fine and nothing shows But with pushReplacement next error shows:
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)
It shows only when I rerender page with "Run 'main.dart'" I wrapped Material app with ScreenUtilInit because
I also run SplashScreen in my main file with InitializerWidget class, if you need more info about it let me know ❤
CodePudding user response:
We should not call functions inside the build
method directly.
Use initState h whenever a logic needs to be implemented only once when the Widget's State is created.
- Change
Stateless
toStateful
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();
}
}
- Now put the required logic inside the
initState
method.
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
}