【问题标题】:Shared Preference in FlutterFlutter 中的共享偏好
【发布时间】:2021-10-11 11:49:00
【问题描述】:

我在获取共享首选项时出错.....

我的代码是...

class _SplashScreenState extends State<SplashScreen> {

  String iddoc = "";

  getStartVal() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    setState(() {
      iddoc = prefs.getString("doctorId");
    });

  if(iddoc == null){
      Timer(Duration(seconds: 5), () => Navigator.push(
        context,
        MaterialPageRoute(builder: (context) => LoginPage()),
      )); 
      
    }else{
      Timer(Duration(seconds: 5), () => Navigator.push(
        context,
        MaterialPageRoute(builder: (context) => MyHomePage()),
      ));  
    }

  }

  @override
  void initState() {
    getStartVal();
    // TODO: implement initState
    super.initState();    
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        fit: StackFit.expand,
        children: <Widget>[
          Container(
            decoration: BoxDecoration(
                  gradient: RadialGradient(
                      colors: [HexColor("#cc1076"), HexColor("#ff72be")]),
                ),
          ),
          Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              Expanded(
                flex: 2,
                child: Container(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      CircleAvatar(
                        backgroundColor: Colors.white,
                        radius: 50.0,
                        child: Image(image: AssetImage('images/logo.png'),),
                      ),
                      Padding(
                        padding: EdgeInsets.only(top: 10.0),
                      ),
                      Text(
                        "OMIS Doc",
                        style: TextStyle(
                            color: Colors.white,
                            fontWeight: FontWeight.bold,
                            fontSize: 24.0),
                      )
                    ],
                  ),
                ),
              ),
              Expanded(
                flex: 1,
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    CircularProgressIndicator(),
                    Padding(
                      padding: EdgeInsets.only(top: 20.0),
                    ),
                    Text(
                      "Please wait.....",
                      softWrap: true,
                      textAlign: TextAlign.center,
                      style: TextStyle(
                          fontWeight: FontWeight.bold,
                          fontSize: 18.0,
                          color: Colors.white),
                    )
                  ],
                ),
              )
            ],
          )
        ],
      ),
    );
  }
}

我的输出是....

'data != null 必须向文本小部件提供非空字符串'

我需要的是使用 if 条件检查是否有任何具有相同键的共享偏好以转到下一页......

但在我的代码中却显示错误.....

为什么会显示?

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    这一定来自您代码的另一部分

    data != null 必须向文本小部件提供非空字符串

    你基本上需要做以下事情

    Text(yourText ?? '')
    

    【讨论】:

      猜你喜欢
      • 2019-03-20
      • 2019-03-18
      • 1970-01-01
      • 1970-01-01
      • 2023-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多