0

This is the class, I want to change the bool selec.

class EleccionConSleccionClase {
      String eleccion;
      bool selec;
    
      EleccionConSleccionClase({
        required this.eleccion,
        this.selec = true,
      });
    }

I have a list of the class before and want to show it while I Can select only one of them. For this I have a StateNotifier (the list comes from another List but it works...). I used the method CambiarSeleccion() for changing to true only the one I want.

class EleccionesConSeleccionNotifier
    extends StateNotifier<List<EleccionConSleccionClase>> {
  final List<EleccionSinSeleccionClase> ListaElecciones;

  EleccionesConSeleccionNotifier({required this.ListaElecciones}) : super([]);

  void init() {
    if (ListaElecciones.length != 0) {
      for (int i = 0; i < ListaElecciones.length; i++) {
        state = ListaElecciones.map(
            (cb) => EleccionConSleccionClase(eleccion: cb.eleccion)).toList();
      }
    }
  }

  void CambiarSeleccion(String nombre) {
    if (ListaElecciones.length != 0) {
      for (int i = 0; i < ListaElecciones.length; i++) {
        if (state[i].eleccion == nombre) {
          state[i].selec = true;
        } else {
          state[i].selec = false;
        }
      }
    }
  }
}

final eleccionConSleccionStateNotifierProvider = StateNotifierProvider<
    EleccionesConSeleccionNotifier, List<EleccionConSleccionClase>>((ref) {
  final eleccioneswatch =
      ref.watch(eleccionesSinSeleccionStateNotifierProvider);

  return EleccionesConSeleccionNotifier(ListaElecciones: eleccioneswatch)
    ..init();
});

The state.selec changes when I touch the button but the UI doesn't update with the new value.

    class EleccionesList extends ConsumerWidget {
      @override
      Widget build(BuildContext context, ScopedReader watch) {
        final eleccioneswatchCon = watch(eleccionConSleccionStateNotifierProvider);
        return Column(
          children: [
            ListView.builder(
                shrinkWrap: true,
                physics: ScrollPhysics(),
                itemCount: eleccioneswatchCon.length,
                itemBuilder: (context, index) {
                  return Row(
                    children: [
                      Text(eleccioneswatchCon[index].eleccion +
                          '    ' +
                          eleccioneswatchCon[index].selec.toString()),
                      IconButton(
                        onPressed: () {                                           
context.read(eleccionConSleccionStateNotifierProvider.notifier).CambiarSeleccion(eleccioneswatchCon[index].eleccion);
                        },
                        icon: Icon(Icons.check_box),
                      )
                    ],
                  );
                }),
            const SizedBox(
              height: 40,
            ),
          ],
        );
      }
    }
  • Instead of `context.read(eleccioneswatchCon)`, probably you want to use `context.read(eleccionConSleccionStateNotifierProvider.notifier)` – Midhun MP Aug 08 '21 at 19:26
  • Yes it work, thank you!, but now the UI doesn't update when I select one of them... The state has changed so the Provider must update normally... – Diego jiménez Aug 08 '21 at 19:50
  • Yes thank you! Now when I touch the button the state changes, but the UI doesn't rebuild like I thought it should. – Diego jiménez Aug 08 '21 at 21:21

0 Answers0