title: How to manage blocs thrown exceptions with StreamBuilder?
body: <p>I'm trying to return a snapshot error state to my StreamBuilder when my provider has problems during the <code>http.get()</code> call. In my case I throw an exception when the the <code>http.get()</code> return a state different from 200 (OK).
  I would like to be able to return a bad state to snapshot and execute the specific code for this situation.
  Now when I throw the exception the application simply crash.</p>
  
  <p>Provider:</p>
  
  <pre><code>class FmsApiProvider {
    Future&lt;List&lt;FmsListResponse&gt;&gt; fetchFmsList() async {
      print("Starting fetch FMS..");
      final Response response = await httpGet('fms');
      if (response.statusCode == HttpStatus.ok) {
        // If the call to the server was successful, parse the JSON
        return fmsListResponseFromJson(response.body);
      } else {
        // If that call was not successful, throw an error.
        //return Future.error(List&lt;FmsListResponse&gt;());
        throw Exception('Failed to load FMSs');
      }
    }
  }
  </code></pre>
  
  <p>Repository:</p>
  
  <pre><code>class Repository {
    final fmsApiProvider = FmsApiProvider();
  
    Future&lt;List&lt;FmsListResponse&gt;&gt; fetchAllFms() =&gt; fmsApiProvider.fetchFmsList();
  }
  </code></pre>
  
  <p>Bloc:</p>
  
  <pre><code>class FmsBloc {
    final _fmsRepository = Repository();
  
    final _fmsFetcher = PublishSubject&lt;List&lt;FmsListResponse&gt;&gt;();
  
    Observable&lt;List&lt;FmsListResponse&gt;&gt; get allFms =&gt; _fmsFetcher.stream;
  
    fetchAllFms() async {
      List&lt;FmsListResponse&gt; itemModel = await _fmsRepository.fetchAllFms();
      _fmsFetcher.sink.add(itemModel);
    }
  
    dispose() {
      _fmsFetcher.close();
    }
  }
  </code></pre>
  
  <p>My StreamBuilder:</p>
  
  <pre><code>StreamBuilder(
              stream: bloc.allFms,
              builder: (context, AsyncSnapshot&lt;List&lt;FmsListResponse&gt;&gt; snapshot) {
                if (snapshot.hasData) {
                  return RefreshIndicator(
                      onRefresh: () async {
                        bloc.fetchAllFms();
                      },
                      color: globals.fcsBlue,
                      child: ScrollConfiguration(
                        behavior: NoOverScrollBehavior(),
                        child: ListView.builder(
                            shrinkWrap: true,
                            itemCount:
                                snapshot.data != null ? snapshot.data.length : 0,
                            itemBuilder: (BuildContext context, int index) {
                              final fms = snapshot.data[index];
                              //Fill a global list that contains the FMS for this instances
                              globals.currentFMSs.add(
                                  FMSBasicInfo(id: fms.id, code: fms.fmsCode));
                              return MyCard(
                                title: _titleContainer(fms.fmsData),
                                fmsId: fms.id,
                                wmId: fms.fmsData.workMachinesList.first
                                    .id, //pass the firs element only for compose the image url
                                imageType: globals.ImageTypeEnum.iteCellLayout,
                                scaleFactor: 4,
                                onPressed: () =&gt; _onPressed(fms),
                              );
                            }),
                      ));
                } else if (snapshot.hasError) {
                  return Text('Fms snapshot error!');
                }
                return FCSLoader();
              })
  </code></pre>
  
  <p>When the exception is thrown I would like to obtain a snapshot error and then visualize only a text in my page.</p>