flutter - 显示从共享偏好到文本小部件颤动的数据列表?
问题描述
我正在将一些数据列表保存到 Sharedprefrence 并尝试从保存的 sharedprefrence 中调用数据并返回我保存的所有值,然后我尝试将 sharedprefrence 中的数据显示到文本小部件,但它显示为 null,
我需要类似的东西,如果我有文本小部件,如何将数据传递给这两个小部件,比如说 ₹575 和 TWA Cap
从 sharedprfence 中检索数据
List<String> listdata=[];
void initState() {
super.initState();
SharedPrefrence().getCartItem().then((data) async{
listdata = data;
print(listdata);
});
}
这就是我从共享偏好中得到的
[TWA Cap, ₹575, M, Red]
试图将数据显示到文本小部件(整个小部件)
Widget CoupensLists() {
return SingleChildScrollView(
physics: NeverScrollableScrollPhysics(),
child: Container(
child: ListView.builder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemCount: 1,
itemBuilder: (BuildContext context, int index) {
return Row(
children: <Widget>[
Expanded(
child: Card(
elevation: 10,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
child: GestureDetector(
onTap: () {
},
child: Container(
height: 150,
width: 350,
child: Row(
children: <Widget>[
Column(
children: <Widget>[
Padding(
padding:
const EdgeInsets.symmetric(vertical: 5),
child: Container(
height: 50,
width: 80,
child: Image.network("image"),
/* decoration: BoxDecoration(
image: DecorationImage(
image: Image.,
fit: BoxFit.fill,
),
),*/
),
),
SizedBox(
height: 5,
),
Text(
"Prodcut name",
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.bold),
),
Text("Prodcut name",
style: TextStyle(fontSize: 12)),
SizedBox(
height: 10,
),
Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.symmetric(
vertical: 4),
child: Container(
width: 80,
child: Stack(
children: <Widget>[
SvgPicture.asset(
'assets/images/bg_price_btn_black.svg',
),
Padding(
padding: const EdgeInsets.all(5),
child: Text(
"Price"
style: TextStyle(
color: Colors.white,
fontSize: 12),
),
)
],
),
),
),
Stack(
children: <Widget>[
SvgPicture.asset(
'assets/images/bg_boon_btn_red.svg',
),
Padding(
padding: const EdgeInsets.all(5),
child: Text(
"Book Now",
style: TextStyle(
color: Colors.white,
fontSize: 12),
),
)
],
),
],
),
],
),
],
),
),
),
),
),
],
);
},
),
),
);
}
解决方案
试试这个:
List<String> listdata=[];
void initState() {
super.initState();
SharedPrefrence().getCartItem().then((data) async{
setState(() {
listdata = data;
});
});
}
你得到这个是因为你在改变 listdata 变量后没有更新你的屏幕
推荐阅读
- php - PHP服务器端隐藏xml到json,结果有不同的格式
- selenium - Selenium Web 驱动程序切换上下文
- javascript - package.json 中的节点引擎 8.x 或 10.x
- regex - 有人可以解释一下,这个程序的第三行到底发生了什么
- excel - Excel:向右移动,然后向下移动
- python - 安装 GraphLab 创建会使我的 Jupyter Notebook 从 Python 3.6 降级到 Python 2.7
- hyperledger-fabric - 现实世界中的超级账本——它如何扩展?
- php - 交叉引用表并将结果插入另一个表 SQL
- python - 对字典的python列表中的值进行排序
- ag-grid - 具有嵌套数组的 ag-grid 数据源