首页 > 解决方案 > initState() 不显示在自动建议中

问题描述

我尝试覆盖initState()inside _SplashScreenState,但它没有显示在建议列表中,我也尝试了无效的捕获/重新启动。

import 'package:coupfferapp/screen/home.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:shimmer/shimmer.dart';

class SplashScreen extends StatefulWidget {
@override
_SplashScreenState createState() => _SplashScreenState();

  }

   class _SplashScreenState extends State<SplashScreen> {



@override
 void initState() {
 super.initState();
_checkLoginSession().then((status) {
  _navigateToHome();
  });
}

Future<bool> _checkLoginSession() async {
await Future.delayed(Duration(microseconds: 5000), () {});
return true;
   }

void _navigateToHome() {
Navigator.of(context).pushReplacement(
    MaterialPageRoute(builder: (BuildContext context) => Home()));
}

 @override
 Widget build(BuildContext context) {
  return Scaffold(
  body: Container(
    child: Stack(
      alignment: Alignment.center,
      children: <Widget>[
        Container(
          color: Colors.white,
        ),
  //            Opacity(
  //                    opacity: .9,
  //                    child: Image.asset('images/splash.png', fit: 
   BoxFit.cover)),
        Shimmer.fromColors(
            child: Container(
              padding: EdgeInsets.all(16.0),
              child: Text(
                "Mayur",
                style: TextStyle(
                    fontSize: 90.0,
                    fontFamily: 'Calistoga',
                    shadows: <Shadow>[
                      Shadow(
                          blurRadius: 18.0,
                          color: Colors.black26,
                          offset: Offset.fromDirection(120, 12))
                    ]),
              ),
            ),
            baseColor: Colors.orange,
            highlightColor: Colors.red)
      ],
     ),
    ),
   );
  }
}

标签: android-studioflutter

解决方案


我遇到了同样的问题,我刚刚从构建函数的正上方调用了我的 initState 函数。让你的 initState() 和构建函数之间没有任何关系。

 @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }
  
  @override
  Widget build(BuildContext context) {
    return isAuth ? buildAuthScreen() : buildUnAuthScreen();
  }

推荐阅读