Cattivo stato: nessun elemento

Sep 23 2020

voglio solo sapere a cosa serve? e quando arriverà, è un problema con le API?

         BlocBuilder<OrderBloc, OrderState>(
                  builder: (context, finalstate) {
                    if (state is OrderLoaded) {
                      for (var item in state.orderSent) {
                        totalweightsforsent.add(item.totalWeight);
//right here when i fetch the data from API and when it is done it is not going forward anymore
                      }
                      for (var item in state.orderPackaging) {
                        totalforpacking.add(item.totalWeight);
                      }

Risposte

3 jitsm555 Sep 23 2020 at 20:33

No, non è un problema API, è lì per qualche motivo

Diamo un'occhiata all'esempio di firstWherecui ottiene la stringa di colore dall'elenco

Stringa di colore non valida

void main() {
  List<String> list = ['red', 'yellow', 'pink', 'blue'];
  var newList = list.firstWhere((element) => element.contains('green'), 
      orElse: () => 'No matching color found');
  print(newList);
}

Produzione:

Nessun colore corrispondente trovato

Stringa di colore valida

 void main() {
      List<String> list = ['red', 'yellow', 'pink', 'blue'];
      var newList = list.firstWhere((element) => element.contains('blue'), 
          orElse: () => 'No matching color found');
      print(newList);
    }

Produzione:

blu

Sopra, sono tutti casi validi, ma se il orElse()blocco viene mancante, verrà lanciatoBadStateException

void main() {
  List<String> list = ['red', 'yellow', 'pink', 'blue'];
  var newList = list.firstWhere((element) => element.contains('green'));
  print(newList);
}

Produzione:

[VERBOSE-2: ui_dart_state.cc (171)] Eccezione non gestita: stato errato: nessun elemento