首页 > 解决方案 > 显示从共享偏好到文本小部件颤动的数据列表?

问题描述

我正在将一些数据列表保存到 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),
                                          ),
                                        )
                                      ],
                                    ),
                                  ],
                                ),
                              ],
                            ),
                           
                          ],
                        ),
                      ),
                    ),
                  ),
                ),
              ],
            );
          },
        ),
      ),
    );
  }

标签: flutter

解决方案


试试这个:

List<String> listdata=[];
  void initState() {
    super.initState();
    SharedPrefrence().getCartItem().then((data) async{      
      setState(() {
        listdata = data;
      });
    });
  }

你得到这个是因为你在改变 listdata 变量后没有更新你的屏幕


推荐阅读