本文介绍了什么是正确的方法来获取已使用sharedPreferences存储到窗口小部件的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获取使用 sharedPreferences 存储到窗口小部件的值的正确方法是什么?



PageA

  @override 
void initState(){
super.initState( );
sharedPreferences = SampleSharedPreferences()。getData();
sharedPreferences.then((onValue){
name = onValue.name; //我调试了这一行,可以看到值
});
}

@override
小部件构建(BuildContext上下文){
return Scaffold(
body:SingleChildScrollView(
child:Column(
crossAxisAlignment:CrossAxisAlignment.start,
子元素:[
填充(
子元素:Text(
名称,
样式:TextStyle(fontSize:20,fontWeight: FontWeight.bold),
),
填充:const EdgeInsets.only(top:14.0,left:14.0),
),
....

错误

 必须为文本窗口小部件提供非空字符串。
'package:flutter / src / widgets / text.dart':
断言失败:第269行第10行:'数据!= null'

我猜是因为小部件在之前先调用调用SharedPreferences 的正确方法是什么?



SampleSharedPreferences

  class SampleSharedPreferences {

static const NAME = user_name;

无效存储(ABC abc)异步{
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString(NAME,名称);
}

Future< ABC> getUser()async {
SharedPreferences prefs = await SharedPreferences.getInstance();
ABC abc =新ABC();
abc.name = prefs.getString(USER_NAME);
返回abc;
}
}


解决方案

您的问题很简单,sharedPreferences会花一些时间来获取保存在其中的值,因为它已经调用了值构建函数,因此它给了您必须为Text小部件提供非null的String。 此错误。



解决方案
,所以请尝试初始化您的Srting名称= = / p>

或支票,

  Text(
name!= null?name:'',
样式:TextStyle(fontSize:20,fontWeight:FontWeight.bold),
),

,并且只要您的String准备好sharepref,就尝试调用setSate,

  sharepref。然后((onValue){
setState((){
mUserInfo = onValue;
});
}


What is the correct way to get the value which has been stored using sharedPreferences to widget?

PageA

 @override
  void initState() {
    super.initState();
    sharedPreferences = SampleSharedPreferences().getData();
    sharedPreferences.then((onValue) {
      name = onValue.name;  // I debug this line, I can see the value
    });
  }

     @override
      Widget build(BuildContext context) {
        return Scaffold(
            body: SingleChildScrollView(
                child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Padding(
              child: Text(
                name,
                style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
              ),
              padding: const EdgeInsets.only(top: 14.0, left: 14.0),
            ),
              ....

Error

A non-null String must be provided to a Text widget.
'package:flutter/src/widgets/text.dart':
Failed assertion: line 269 pos 10: 'data != null'

I guess is because the widget is calling first before SharedPreferences get called? What is the proper way to write it?

SampleSharedPreferences

class SampleSharedPreferences {

  static const NAME = "user_name";

  void store(ABC abc) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setString(NAME, name);
  }

  Future<ABC> getUser() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    ABC abc = new ABC();
    abc.name = prefs.getString(USER_NAME);
    return abc;
  }
}
解决方案

Your problem is simple sharedPreferences takes some time to fetch values saved in it, by the time it fetches the value build function is already called and hence it gives you A non-null String must be provided to a Text widget. this error.

Solution:so either try to initialize your Srting name=""

or check,

Text(
  name!=null? name:'',
  style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
  ),

and whenever sharepref is ready with your String try to call setSate,

 sharepref .then((onValue) {
      setState(() {
        mUserInfo = onValue;
      });
    }

这篇关于什么是正确的方法来获取已使用sharedPreferences存储到窗口小部件的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 01:51