1

I have a mobile frontend app for a web service, requiring a user login.

Login tokens can expire or invalidate at any moment returning a particular http error code.

I'm not sure how I should handle this in Flutter.

I could add a try-catch to all web calls but it sounds like a lot of redundant typing and copy-pasting.

I'm thinking about registering a listener for the API service class but how do I get a flutter context in the callback for route management?

I've checked this question. Mine is not about how to check for errors or log out the user, but rather how to centralize/convert the existing app?

Another question looks like my case but the only answer is not accepted after many months. Makes me doubt if that's how it should be done.

Any ideas appreciated

Anton Duzenko
  • 2,366
  • 1
  • 21
  • 26

2 Answers2

1

First , You should have request responses passed through the same method. When you get the error code, you then call your ChangeNotifier to rebuild the Consumer in your App.

Future<ApiResponse> makeGetRequest(url) async {
var response = await client.get(url, headers: await headers());
return _response(response);
}

dynamic _response(http.Response response) {
switch (response.statusCode) {
  case 200:
    return ApiResponse.fromJson(json.decode(response.body));
  case 401:
    locator<GlobalChangeNotifier>().logout();
  default:
    throw ErrorHandler(response);

}

So you can catch all Headers for GET requests, You can use this same approach for you http.client and make all requests go through your client, you'd have to pass the request type through your method.

Tiwa Babalola
  • 265
  • 5
  • 10
  • What about routes/navigation stack? – Anton Duzenko Aug 09 '20 at 19:01
  • I'm trying to implement this. I already have my requests going through a single method and a logout method cleaning stuff and sending client to login. My problem is that even when a push the app to the login screen on a 401 or 403 return from the api, flutter still executes code from the screen that were called before the login screen redirect. What exactly would your logout look like ? Does invoking it through changenotifier somehow interupts code execution and sends the client to the login page without executing code from process being executed prior to the API request ? Thanks in advance. – Caio Sant'Anna May 20 '22 at 14:14
0

Suppose there is a generateroute which controlls all routing

runApp(new Material (
        child: MaterialApp(
          title: 'Title',
          debugShowCheckedModeBanner: false,
          theme: App.theme,
          home: Splash(),
          onGenerateRoute: router.generateRoute,
        )
      )
  )

Now in router class

Route<dynamic> generateRoute(RouteSettings settings) {

// Add a logic here. Save the GlobalStatus.authStatus whenever a http call is made.
if(GlobalStatus.authStatus==401)
  return MaterialPageRoute(builder: (context) => Login());

  switch (settings.name) {
...
...
Dev
  • 6,628
  • 2
  • 25
  • 34
  • Would it only check when a navigation event occurs? I would like it to logout right after an error occurs, not when user leaves current page. – Anton Duzenko Feb 10 '20 at 11:46
  • for that you have to use some observables. the mobx thing you referred is correct way to go but in turn might need code changes – Dev Feb 10 '20 at 11:48