简体   繁体   English

Flutter 安全存储未按应用程序启动时的方式初始化

[英]Flutter Secure Storage not initializing as itended on app start up

I am working on an application with dynamic splash screen images.我正在开发一个带有动态启动画面图像的应用程序。 This is how I have implemented it.这就是我实现它的方式。


class _SplashScreenState extends State<SplashScreen> {

  loginState() async{
    if(await FlutterSecureStorage().read(key: "Login")!=null){
      isLoggedIn = true;
      image = await StorageAccess().readFile();

    }
  }
  bool isLoggedIn = false;
  var image;
  @override
  void initState() {
    loginState();
    super.initState();
    Timer(Duration(seconds: 4),
            ()=>Navigator.pushReplacement(context,
            MaterialPageRoute(builder:
                (context) =>
                FirstPage()
            )
        )
    );
  }
  @override
  Widget build(BuildContext context) {
    return isLoggedIn?Container(
        color: Colors.white,
        child:Image.memory(image),
    ):Container(
      color: Colors.white,
      child: Image.asset('images/logo.png'),
    );
  }
}

This works properly when I perform a hot restart but when I close and start the application variable isLoggedIn is taking way more time to get initialised and my dynamic image in the splash screen is not showing.这在我执行热重启时可以正常工作,但是当我关闭并启动应用程序变量 isLoggedIn 需要更多时间来初始化并且我在初始屏幕中的动态图像没有显示。

Anyone has any idea why it takes so much time to initialize on app boot up but not on hot restart and how do I fix this?任何人都知道为什么在应用程序启动时初始化而不是热重启需要这么多时间,我该如何解决这个问题?

Thanks In Advance.提前致谢。

you can try run initState with async.您可以尝试使用异步运行 initState。

void initState() async {
loginState();
super.initState();
Timer(Duration(seconds: 4),
        ()=>Navigator.pushReplacement(context,
        MaterialPageRoute(builder:
            (context) =>
            FirstPage()
        )
    )
);

} }

similar problem I'm struggling too.类似的问题我也在苦苦挣扎。 For my case, I'm using getx, so that this may solve the problem you get.就我而言,我使用的是 getx,这样可以解决您遇到的问题。

I used FutureBuilder to sort out this issue.我使用 FutureBuilder 来解决这个问题。 Updated code is like this:更新后的代码是这样的:

class _SplashScreenState extends State<SplashScreen> {

  loginState() async{
    if(await FlutterSecureStorage().read(key: "Login")!=null){
      isLoggedIn = true;
      image = await StorageAccess().readFile();

    }
  }
  bool isLoggedIn = false;
  var image;
  @override
  void initState() {
    super.initState();
    Timer(Duration(seconds: 4),
            ()=>Navigator.pushReplacement(context,
            MaterialPageRoute(builder:
                (context) =>
                FirstPage()
            )
        )
    );
  }
  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: loginState(),
      builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
        return isLoggedIn?Container(
          color: Colors.white,
          child:Image.memory(image),
        ):Container(
          color: Colors.white,
          child: Image.asset('images/logo.png'),
        );
    },);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM