Mau estado: nenhum elemento

Sep 23 2020

eu só quero saber para que serve isso? e quando chegar, é um problema de 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);
                      }

Respostas

3 jitsm555 Sep 23 2020 at 20:33

Não, não é um problema de API, está lá por algum motivo

Vejamos o exemplo firstWhereque obtém a string de cores da lista

String de cor inválida

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

Resultado:

Nenhuma cor correspondente encontrada

String de cor válida

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

Resultado:

azul

Acima, todos são casos válidos, mas se o orElse()bloco ficar faltando lá, ele lançaráBadStateException

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

Resultado:

[VERBOSE-2: ui_dart_state.cc (171)] Exceção não tratada: Estado incorreto: nenhum elemento