0

I am generating a set for selected items on tap of the iconbutton. But only one item is added in set and others are removed.

This is a code for selecting favorite items from the image grid created from a image list. When tapping on the Icon itemname should be added to the set. Right now when i tap on the icon it adds the item in the set and tapping other item it is removing previous item and adds new item. So at all times set is either empty or having only one item.

class TravelDestinationItem extends StatefulWidget {
  TravelDestinationItem({Key key, @required this.destination, this.shape})
      : assert(destination != null),
        super(key: key);

  static const double height = 566.0;
  final Item destination;
  final ShapeBorder shape;
  final Set<String> saved = Set<String>();
  @override
  _TravelDestinationItemState createState() => _TravelDestinationItemState();
}

class _TravelDestinationItemState extends State<TravelDestinationItem> {


  @override
  Widget build(BuildContext context) {
    final ThemeData theme = Theme.of(context);
    final TextStyle titleStyle =
        theme.textTheme.headline.copyWith(color: Colors.white);
    final TextStyle descriptionStyle = theme.textTheme.subhead;


   final String itemname=widget.destination.itemname;

   final bool alreadySaved = widget.saved.contains(itemname);



    return SafeArea(
        top: false,
        bottom: false,
        child: GestureDetector(

        child: Container(
          padding: const EdgeInsets.all(4.0),
          height: TravelDestinationItem.height,
          child: Card(
            shape: widget.shape,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[

                SizedBox(
                  height: 150.0,
                  child: Stack(
                    children: <Widget>[
                      Positioned.fill(
                        child: Image.asset(
                          widget.destination.imagename,

                          fit: BoxFit.scaleDown,
                        ),
                      ),
                      Container(
                          alignment: Alignment.topLeft,

                          child: IconButton(icon:Icon(

                            alreadySaved
                                ? Icons.favorite
                                : Icons.favorite_border,
                            color: alreadySaved ? Colors.red : null,
                          ),
                          onPressed : (){setState((){
                          if (alreadySaved) {
                            widget.saved.remove(itemname);
                          } else {
                            widget.saved.add(itemname);
                            print(widget.saved);
                          }
                          });}
                          ),

                          ),
                    ],
                  ),
                ),

                Expanded(
                  child: Container(
                    padding: const EdgeInsets.fromLTRB(16.0, 16.0, 16.0, 0.0),
                    child: DefaultTextStyle(
                      style: descriptionStyle,
                      child: Row(
                        mainAxisSize: MainAxisSize.max,
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: <Widget>[

                          Padding(
                            padding: const EdgeInsets.only(bottom: 8.0),
                            child: Text(
                              widget.destination.itemname,
                              style: descriptionStyle.copyWith(
                                  color: Colors.black87),

                            ),
                          ),

                        ],
                      ),
                    ),
                  ),
                ),

              ],
            ),
          ),
        )));
  }
}
2
  • try moving set from widget class to state class Aug 14, 2019 at 12:39
  • If you are saying, about shifting class TravelDestinationItem extends StatefulWidget {.....final Set<String> saved = Set<String>();... to class _TravelDestinationItemState extends State<TravelDestinationItem> {..., it was there only, but i thought of scope and put it in TravelDestinationItem class. Its not working Aug 14, 2019 at 12:42

1 Answer 1

2

Anything contained inside the StatefulWidget subclass (not State) must be immutable.

You cannot do something like widget.mySet.add(something). This will cause your state to be lost uncontrollably.

Instead have the State subclass store internally a Set, and use it

4
  • Can you please guide , how to do it? Aug 14, 2019 at 12:48
  • @JatanPandya Simply move saved from TravelDestinationItem to _TravelDestinationItemState Aug 14, 2019 at 12:49
  • It was there only previously. i moved it in state class again, but still set holds only one item or remains empty. Aug 14, 2019 at 12:51
  • Here is the output in console, generated from print in else part written in setState(), I/flutter ( 6959): {Item1} -- Tapped on Item1 I/flutter ( 6959): {Item2} -- Tapped on item2 I/flutter ( 6959): {Item3} -- Tapped on item3 I/flutter ( 6959): {Item1} --tapped on item1 again Aug 14, 2019 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.