Abhijith
Abhijith

Reputation: 2327

Display list of datas from sharedprefrence to text widget flutter?

I am saving some list of data to Sharedprefrence and tried to call the data from saved sharedprefrence and it returs all the values i have saved,then i tried to show data from sharedprefrence to a text widget but it shows null,

i need something like,if i have t text widgets how do pass data to those two widget let's say ₹575 and TWA Cap

Retrieving the data from sharedprfrence

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

this what i am geting from the sharedprefrence

[TWA Cap, ₹575, M, Red]

trying to shows te data to text widget (Whole widget)

     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),
                                          ),
                                        )
                                      ],
                                    ),
                                  ],
                                ),
                              ],
                            ),
                           
                          ],
                        ),
                      ),
                    ),
                  ),
                ),
              ],
            );
          },
        ),
      ),
    );
  }

Upvotes: 0

Views: 52

Answers (2)

Johny Saini
Johny Saini

Reputation: 909

use in initail function    
setState(() {
    listdata = data;
});

then

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(
                            listdata.length!=0? listdata[0]:"",
                              style: TextStyle(
                                  fontSize: 14,
                                  fontWeight: FontWeight.bold),
                            ),
                            Text( listdata.length!=0? listdata[1]:"",
                                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(
                                             listdata.length!=0? listdata[2]:"",
                                            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(
                                        listdata.length!=0? listdata[3]:"",
                                        style: TextStyle(
                                            color: Colors.white,
                                            fontSize: 12),
                                      ),
                                    )
                                  ],
                                ),
                              ],
                            ),
                          ],
                        ),
                       
                      ],
                    ),
                  ),
                ),
              ),
            ),
          ],
        );
      },
    ),``
  ),
);

}

Upvotes: 0

Vitor
Vitor

Reputation: 840

Try this:

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

You're getting this because you didn't upated your screen after changing the listdata variable

Upvotes: 1

Related Questions