检查键是否存在共享的首选项值,然后打开一个新屏幕

问题描述 投票:0回答:2

如果共享首选项中存在电子邮件值,我想打开一个新屏幕

我要呼叫电子邮件功能并获取电子邮件功能的值,并检查其是否包含任何值如果email()不为空,则重定向到新屏幕

class SplashScreen extends StatefulWidget {
  SharedPreferences sharedPreferences;
  Future<String> email() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    return await preferences.getString("email");
  }

  @override
  _SplashScreenState createState() => _SplashScreenState();
}

class _SplashScreenState extends State<SplashScreen>
    with SingleTickerProviderStateMixin {
  @override
  void initState() {
    super.initState();
    Future.delayed(
      Duration(seconds: 3),
      () {
        Navigator.pushReplacement(
            context,
            PageRouteBuilder(
                transitionDuration: Duration(milliseconds: 1500),
                pageBuilder: (_, __, ___) => Introduction_Screen()));
      },
    );
  }


flutter sharedpreferences
2个回答
0
投票

您可以这样做。.

调用initState中的函数。

void initState() {
super.initState();
email();
}

And..in email()

void email(){
SharedPreferences preferences = await SharedPreferences.getInstance();
var a = preferences.getString("email");
if(a != null && !a.isEmpty){
//Navigate to one screen
} else {
//Navigate to another screen
}
}

希望它能回答您的问题。


0
投票

您可以执行此操作。

void init(){
super.init();
email();
}

void email() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    var email =  await preferences.getString("email");
     if (email?.isNotEmpty ?? true) {
          Navigator.pushReplacement(
            context,
            PageRouteBuilder(
                transitionDuration: Duration(milliseconds: 1500),
                pageBuilder: (_, __, ___) => Introduction_Screen()));
    }
    else{
       //navigaate to some other screen
     }
  } 
© www.soinside.com 2019 - 2024. All rights reserved.