如何检查密钥是否存在共享首选项值,然后打开一个新屏幕

how to check if key exist shared preferences value and then open a new screen

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

我想调用电子邮件功能
并获取 email 函数的值并检查它是否包含任何值
如果 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()));
      },
    );
  }

 

你可以这样做..

调用initState中的函数..

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

并且..在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
}
}

希望它能回答您的问题。

你可以做到这一点。

void initState(){
super.initState();
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
     }
  }