Flutter:在构建期间调用的serState或markNeedBuild

问题描述

  • 我有一个用于选择日期的小部件,开始时带有“未选择日期”,它下面一列中的小部件依赖于此[这意味着它会显示一个timeSlots of Data if我选择了一个日期]

  • 但是在构建错误期间我得到了一个setState调用,并且这个ReservationSlotWidget无法标记为需要构建,因为框架已经在构建窗口小部件了

  • 这是selectDate小部件

 child: Column(
            mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,children: <Widget>[
                  Row(
                    children: <Widget>[
                      Icon(
                        Icons.date_range,color: Colors.white,),SizedBox(
                        width: 5,Text(
                        _formatted == null
                        ? 'No date chosen'
                        : _formatted,style: TextStyle(
                            color: Colors.white,))
                    ],FlatButton(
                    onpressed: ()=>_selectReservationDate(context),child: Text(
                      'choose Date',style: TextStyle(
                          color: Colors.white,decoration: Textdecoration.underline),)
                ],if(_selectedDate != null)
              Divider(
                color: parseColor('#2A2E43'),thickness: 2.0,if(_selectedDate != null)
              Row(
                children: <Widget>[
                  Icon(
                    Icons.alarm,SizedBox(
                    width: 5,Text('$_numberOfSeats Available Time Slots',style: TextStyle(
                        color: Colors.white,))
                ],
  • 这是一个列表视图项,取决于我选择的日期。

              if(_formatted != null)
         
  Expanded(
                  child: Consumer<SlotProvider>(
                    builder: (ctx,slotProvider,_)=>
                     ListView.builder(
                itemCount: slotProvider.slots.length,scrollDirection: Axis.horizontal,itemBuilder: (ctx,index) {
                    setState(() {
                      _numberOfSeats = slotProvider.slots[index].seats;
                    });
                    return GestureDetector(
                      onTap: () {
                        setState(() {
                          if(_isInit){
                          _isExpanded = !_isExpanded;
                          }
                           _expandedindex = index;
                          _isInit = false;
                        });
                      },child: Container(
                          padding: EdgeInsets.all(4),margin: EdgeInsets.all(4),decoration: Boxdecoration(
                              color: _expandedindex == index
                                  ? Theme.of(context).primaryColor
                                  : parseColor('#2A2E43'),borderRadius:
                                  BorderRadius.all(Radius.circular(10))),child: SlotsWidget(
                            fromTime: slotProvider.slots[index].fromTime.toString(),toTime:slotProvider.slots[index].toTime.toString(),seats:slotProvider.slots[index].seats,))
                          );
                          }
              ),)
              ),

解决方法

@void的注释是正确的,可以通过if块避免重复的setState;

if (_numberOfSeats != slotProvider.slots[index].seats)
   setState(() {
      _numberOfSeats = slotProvider.slots[index].seats;
   });