title: How to redirect to a login page if Flutter API response is unauthorized?
body: <p>I am building a Flutter app which uses a Golang API to fetch data. The API will return a 401 unauthorized if the JWT token is not valid. How can I redirect to a login page on any API call if the response status is 401?</p>
  <p>Here is my flutter code:</p>
  <p>main.dart</p>
  <pre><code>void main() async {
  
    WidgetsFlutterBinding.ensureInitialized();
  
    Provider.debugCheckInvalidValueType = null;
  
    AppLanguage appLanguage = AppLanguage();
    
    await appLanguage.fetchLocale();
    
    runApp(MyApp(
      appLanguage: appLanguage,
    ));
  
  }
  
  class MyApp extends StatelessWidget {
    
    final AppLanguage appLanguage;
  
    MyApp({this.appLanguage});
  
    @override
    Widget build(BuildContext context) {
  
      return  MultiProvider(
            
                    providers: providers,
  
                    child: MaterialApp(
                              
                        localizationsDelegates: [
                           AppLocalizations.delegate,
                           GlobalMaterialLocalizations.delegate,
                           GlobalWidgetsLocalizations.delegate,
                        ],
            
                    initialRoute: RoutePaths.Login,
                    onGenerateRoute: Router.generateRoute,
                    )        
                 );
          }  
  }
  </code></pre>
  <p>tables.dart</p>
  <pre><code>class Tables extends StatelessWidget {
  
    const Tables({Key key}) : super(key: key);
  
    @override
    Widget build(BuildContext context) {
  
      return BaseWidget&lt;TablesModel&gt;(
      
          model: TablesModel(api: Provider.of(context, listen: false)),
      
          onModelReady: (model) =&gt; model.fetchTables(),
      
          builder: (context, model, child) =&gt; model.busy
  
              ? Center(
  
                  child: CircularProgressIndicator(),
                
                )
          
              : Expanded(
                 
                   child: GridView.builder (
  ---
  </code></pre>
  <p>tables_model.dart</p>
  <pre><code>class TablesModel extends BaseModel {
    
    Api _api;
  
    TablesModel({@required Api api}) : _api = api;
  
    List&lt;Tbl&gt; tables;
  
    Future fetchTables() async {
      setBusy(true);
      tables = await _api.getTables();
      setBusy(false);
    }
  
    @override
    void dispose() {
      print('Tables has been disposed!!');
      super.dispose();
    }
  }
  </code></pre>
  <p>api.dart</p>
  <pre><code>Future&lt;List&lt;Tbl&gt;&gt; getTables() async {
      
      var tables = List&lt;Tbl&gt;();
  
      try {
  
          var response = await http.get('$_baseUrl/tables/list');
  
          var parsed = json.decode(response.body) as List&lt;dynamic&gt;;
      
          if (parsed != null) {
  
             for (var table in parsed) {
      
                 tables.add(Tbl.fromJson(table));
      
             }
          }
  
      } catch (e) {print(e); return null;}
  
      return tables;
    }
  </code></pre>