Splash Screen does not appear in the flutter app

Asked

Viewed 38 times

-2

Hi, I’m having trouble showing Splash Screen on my flutter app and would like help. When opening the application it appears, but is only visible for 1 second, even if I put a 3-second timer. This occurred apparently after you added Sharedpreferences.

It follows the code: main.Dart

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<UserModel>(
      create: (context) => UserModel(),
      child: Consumer<UserModel>(
        builder: (context, value, child) {
          return MaterialApp(
            title: 'Onde Está?',
            debugShowCheckedModeBanner: false,
           
            home: SplashPage(),
          );
        },
      ),
    );
  }
}

splash_page.Dart

import 'dart:async';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter/material.dart';
import 'package:onde_esta/app/share/constant.dart';
import 'package:onde_esta/app/views/list_object.dart';
import 'package:onde_esta/app/widgets/home_page.dart';

String finalEmail;

class SplashPage extends StatefulWidget {
  @override
  SplashPageState createState() => SplashPageState();
}

class SplashPageState extends State<SplashPage> {
  Future getValidationDate() async {
    final SharedPreferences sharedPreferences =
        await SharedPreferences.getInstance();
    var obtainedEmail = sharedPreferences.getString('email');
    setState(() {
      finalEmail = obtainedEmail;
    });
    print('$finalEmail');
  }

  @override
  void initState() {
    getValidationDate().whenComplete(() async {
      Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) {
        return finalEmail == null ? HomePage() : ObjectListScreen();
      }));
    });
    super.initState();
    Timer(
      Duration(seconds: 3),
      () => Navigator.pushReplacement(
        context,
        MaterialPageRoute(builder: (context) => HomePage()),
      ),
    );
  }

  @override
  Widget build...

Console error:

E/flutter (11382): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: This widget has been unmounted, so the State no longer has a context (and should be considered defunct).
E/flutter (11382): Consider canceling any active work during "dispose" or using the "mounted" getter to determine if the State is still active.
E/flutter (11382): #0      State.context.<anonymous closure> 
package:flutter/…/widgets/framework.dart:1088
E/flutter (11382): #1      State.context 
package:flutter/…/widgets/framework.dart:1094
E/flutter (11382): #2      SplashPageState.initState.<anonymous closure> 
package:onde_esta/…/widgets/splash_page.dart:37
E/flutter (11382): #3      _rootRun  (dart:async/zone.dart:1346:47)
E/flutter (11382): #4      _CustomZone.run  (dart:async/zone.dart:1258:19)
E/flutter (11382): #5      _CustomZone.runGuarded  (dart:async/zone.dart:1162:7)
E/flutter (11382): #6      _CustomZone.bindCallbackGuarded.<anonymous closure>  (dart:async/zone.dart:1202:23)
E/flutter (11382): #7      _rootRun  (dart:async/zone.dart:1354:13)
E/flutter (11382): #8      _CustomZone.run  (dart:async/zone.dart:1258:19)
E/flutter (11382): #9      _CustomZone.bindCallback.<anonymous closure>  (dart:async/zone.dart:1186:23)
E/flutter (11382): #10     Timer._createTimer.<anonymous closure>  (dart:async-patch/timer_patch.dart:18:15)
E/flutter (11382): #11     _Timer._runTimers  (dart:isolate-patch/timer_impl.dart:395:19)
E/flutter (11382): #12     _Timer._handleMessage  (dart:isolate-patch/timer_impl.dart:426:5)
E/flutter (11382): #13     _RawReceivePortImpl._handleMessage  (dart:isolate-patch/isolate_patch.dart:184:12)

1 answer

0


The error is occurring because the getValidationDate() method is running before the screen is built and so there is no way to update the screen with the setstate before the screen is built, instead of using Timer use Future delayed.

  • Thank you! It worked for me!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.