0

In this example, I'm trying to understand whether I need to use a FutureBuilder, in order to have the value of the boolean "selected" available for my onTap method.

return Card(
        color: Color.fromARGB(255, 233, 30, 99),
        elevation: 5.0,
        child: InkWell(
          splashColor: Colors.blueGrey,
          onTap: () {
              return setState(
                () {
                  this.selected = !this.selected;
                  selected == true
                      ? model.disAllowTouchSounds()
                      : model.allowTouchSounds();
                },
              );
          },
          child: Padding(
            padding: EdgeInsets.only(left: 10.0, right: 10.0),
            child: StreamBuilder<String>(
              stream: stream1,
              builder: (context, snapshot1) {
                return StreamBuilder<String>(
                  stream: stream2,
                  builder: (context, snapshot2) {
                    if (snapshot1.hasData && snapshot2.hasData) {
                      if (snapshot1.data
                              .toString()
                              .contains('TOUCH SOUNDS ALLOWED') ||
                          snapshot2.data.toString().contains('DND OFF')) {
                        selected = false;
                        return TouchOn();
                      } else if (snapshot2.data
                              .toString()
                              .contains('ALARMS ONLY ON') ||
                          snapshot2.data.toString().contains('DND ON')) {
                        selected = true;
                        isDisabled = true;
                        return TouchOff();
                      }
                    }

As my Streambuilder probably takes a little while to get built, I am wondering whether the this.selected = !this.selected can work properly as it is.

The reason I am asking is that I occasionally have to tap twice for the disAllowTouchSounds() method to fire. Ideally, I would want to avoid this situation.

Code Poet
  • 6,222
  • 2
  • 29
  • 50

1 Answers1

0

this.selected = !this.selected happens inside setState() so it should always work. Make your StreamBuilder builder function set selected inside a setState() call.

M. Leonhard
  • 1,332
  • 1
  • 18
  • 20
  • Thanks, I tried it but this is what I get: I/flutter ( 6688): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════ I/flutter ( 6688): The following assertion was thrown building StreamBuilder(dirty, state: I/flutter ( 6688): _StreamBuilderBaseState>#505b0): I/flutter ( 6688): setState() or markNeedsBuild() called during build. I/flutter ( 6688): This GridCardSeven widget cannot be marked as needing to build because the framework is already in I/flutter ( 6688): the process of building widgets. – Code Poet Mar 12 '19 at 07:35