DPLYR-dev / SplashScreenFlutterPackage

A small splashscreen used as intro for flutter applications easily with a lot of customizations ❤️🥳
MIT License
294 stars 122 forks source link

After updating to Null Safety, Error: This widget has been unmounted, so the State no longer has a context #74

Closed gOzaru closed 2 years ago

gOzaru commented 3 years ago

Describe the bug I have been using the latest package of Null Safety since 4 months ago and so far, I have no error. Today, I got errors:

FlutterError (This widget has been unmounted, so the State no longer has a context (and should be considered defunct).
Consider canceling any active work during "dispose" or using the "mounted" getter to determine if the State is still active.)

on this line of codes in splashscreen.dart:

else if (widget.navigateAfterSeconds is Widget) {
            Navigator.of(context).pushReplacement(   <= Error in context, that has been missing
              widget.pageRoute != null
                  ? widget.pageRoute!
                  : MaterialPageRoute(
                      settings: widget.routeName != null ? RouteSettings(name: widget.routeName) : null,
                      builder: (context) => widget.navigateAfterSeconds as Widget,
                    ),
            );
          }

This is my lines of code:

class Splash extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return SplashScreen.timer(
      seconds: 5,
      navigateAfterSeconds: new SignIn(),
      title: Text(
        'HaD',
        textAlign: TextAlign.center,
        style: TextStyle(color: Colors.white, fontSize: 32.sp, fontWeight: FontWeight.bold),
      ),
      image: Image.asset("assets/images/logohad.png"),
      loadingText: Text(
        "PT Sayap Impian Merpati",
        style: TextStyle(color: Colors.white, fontSize: 16.sp),
        textAlign: TextAlign.center,
      ),
      photoSize: 100.sp,
      loaderColor: Colors.blue,
      backgroundColor: Colors.black,
      useLoader: true,
      loadingTextPadding: EdgeInsets.all(10.0),
      styleTextUnderTheLoader: TextStyle(color: Colors.white, fontSize: 18.sp),
    );
  }
}

To Reproduce Steps to reproduce the behavior:

  1. Call Splash() function to create a Splash Screen which navigates to SignIn()
  2. Get error when running the Splash() function above (The loader animation is stopped)

Expected behavior There should be no error at all since I have been using it since 4 months ago until today, and yet it gets error suddenly. Maybe that is because I used the latest package of flutter_screenutil.

Desktop

Smartphone

gOzaru commented 3 years ago

No need. I found the solution. I copied the library into my project and removed all navigateAfterFuture variable and all of its assertions. I only used the navigateAfterSeconds, changed its variable into Widget? and only allowed for Widget?, not for route name. I also used Getx Route Management for easier use and added symbol ! to the variable of navigateAfterSeconds.

I declared the variable as: late Widget? navOne = null;

on initState():

if (widget.navigateAfterSeconds != null) {
      code = 1;
      navOne = widget.navigateAfterSeconds;
}

inside Widget build(BuildContext context): Timer(Duration(seconds: widget.seconds!), () => {Get.offAll(() => navOne!)});

Now, my splashscreen is running as I want it to be. Thanks to God who helped me out in this problem. I needed to figure this out at least in 12 hours. We need to add the symbol ! to related variable to change the whole package into completely Null Safety. Since I can't change the online library, I copied it into my project and now all is perfect. Thank you to whoever made this package. God bless you in health and wealth.

Note: Once I get succeed in the future, I promise that I will give 10% of a total $1,000,000 to all authors whom created any package inside the project I have been working on. You guys made this for free, and I think all of you deserve the money I will earn later. Keep working to make Flutter easier to work on for newbies like me. God bless you all in health and wealth. Cheers.