title: How to show errors from ChangeNotifier using Provider in Flutter
body: <p>I'm trying to find the best way to show errors from a Change Notifier Model with Provider through a Snackbar.</p>
  <p>Is there any built-in way or any advice you could help me with?</p>
  <p>I found this way that is working but I don't know if it's correct.</p>
  <p>Suppose I have a simple Page where I want to display a list of objects and a Model where I retrieve those objects from api. In case of error I notify an error String and I would like to display that error with a SnackBar.</p>
  <h1>page.dart</h1>
  <pre><code>import 'package:flutter/material.dart';
  import 'package:provider/provider.dart';
  
  class Page extends StatefulWidget {
    Page({Key key}) : super(key: key);
  
    @override
    _PageState createState() =&gt; _PageState();
  }
  
  class _PageState extends State&lt; Page &gt; {
  
    @override
    void initState(){
      super.initState();
      Provider.of&lt;Model&gt;(context, listen: false).load();
    }
  
    @override
    void didChangeDependencies() {
      super.didChangeDependencies();
      Provider.of&lt; Model &gt;(context, listen: false).addListener(_listenForErrors);
    }
  
    @override
    Widget build(BuildContext context){
      super.build(context);
      return Scaffold(
        appBar: AppBar(),
        body: Consumer&lt;Model&gt;(
            builder: (context, model, child){
    
              if(model.elements != null){
                ...list
              }
              else return LoadingWidget();
            }
          )
        )
      );
    }
  
  
  
    void _listenForErrors(){
      final error = Provider.of&lt;Model&gt;(context, listen: false).error;
      if (error != null) {
        Scaffold.of(context)
          ..hideCurrentSnackBar()
          ..showSnackBar(
            SnackBar(
              backgroundColor: Colors.red[600],
              content: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Icon(Icons.error),
                  Expanded(child: Padding( padding:EdgeInsets.only(left:16), child:Text(error) )),
                ],
              ),
            ),
          );
      }
    }
  
    @override
    void dispose() { 
          Provider.of&lt;PushNotificationModel&gt;(context, listen: false).removeListener(_listenForErrors);
      super.dispose();
    }
  
  }
  </code></pre>
  <h1>page_model.dart</h1>
  <pre><code>import 'package:flutter/foundation.dart';
  
  class BrickModel extends ChangeNotifier {
  
    List&lt;String&gt; _elements;
    List&lt;String&gt; get elements =&gt; _elements;
  
    String _error;
    String get error =&gt; _error;
  
    Future&lt;void&gt; load() async {
      try{
        final elements = await someApiCall();
        _elements = [..._elements, ...elements];
      }
      catch(e) {
        _error = e.toString();
      }
      finally {
        notifyListeners();
      }
    }
  
  }
  </code></pre>
  <p>Thank you</p>