Home > Enterprise >  Why doesn't my method work in DioError catch in Flutter/Dart?
Why doesn't my method work in DioError catch in Flutter/Dart?

Time:07-31

I am making a request to my database like this:

//Airtable (find a record)
  void airtableFind() async {
    try {
      final response = await Dio().get(
        'https://api.airtable.com/v0/' projectBase '/' recordName,
        queryParameters: {
          'filterByFormula': 'SEARCH(' '"' username '"' ',{Name})' // Searches the value 'Cactus' in the {'Short description'} field.
        },
        options: Options(
          contentType: 'Application/json',
          headers: {
            'Authorization': 'Bearer' ' ' apiKey,
            'Accept': 'Application/json',
          },
        ),
      );

      // TODO: Whatever you want to do with the response. A good practice is to transform it into models and than work with them
      // print(response);
      // print(response.data['records'][0]['id']);
      idString = response.data['records'][0]['id'];
      // if (idString.isNotEmpty) (
      //     showInvalidUsernameDialog(context)
      //     // TODO: Need to inform about success
      // );

    } on DioError catch (e) {
      // TODO: Error handling
      if (e.response != null) {
        // print(e.response.data);
        print(e);
        showInvalidUsernameDialog(context);

      } else {
        // print(e.request);
        print(e.message);
        showInvalidUsernameDialog(context);
      }
    }
  }

If my user enters the correct word (username), then everything works correctly. But there is always a risk that a person is mistaken. And I want to indicate this with the showInvalidUsernameDialog(context); dialog box, but for some reason it does not pop up.

I see errors in the console:

I/flutter(4484): DioError [DioErrorType.response]: Http status error [422]
I/flutter ( 4484): Source stack:
I/flutter(4484): #0 DioMixin.fetch(package:dio/src/dio_mixin.dart:488:35)
I/flutter ( 4484): #1 DioMixin.request (package:dio/src/dio_mixin.dart:483:12)
I/flutter ( 4484): #2 DioMixin.patch (package:dio/src/dio_mixin.dart:249:12)
I/flutter(4484): #3 _RouteState.airtableUpdate(package:example/main.dart:1498:36)
I/flutter ( 4484): #4 _RouteState.build.<anonymous closure> (package:example/main.dart:1617:13)
I/flutter ( 4484): #5 _RouteState.build.<anonymous closure> (package:example/main.dart:1612:24)
I/flutter(4484): #6 EditableTextState._finalizeEditing (package:flutter/src/widgets/editable_text.dart:2148:18)
I/flutter(4484): #7 EditableTextState.performAction(package:flutter/src/widgets/editable_text.dart:1999:9)
I/flutter(4484): #8 TextInput._handleTextInputInvocation(package:flutter/src/services/text_input.dart:1746:37)
I/flutter ( 4484): #9 MethodChannel._handleAsMethodCall (package:flutter/src/services/platform_channel.dart:404:55)
I/flutter ( 4484): #10 MethodChannel.setMethodCallHandler.<anonymous closure> (package:flutter/src/services/platform_chan
E/flutter ( 4484): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: RangeError (index): Invalid value: Valid value range is empty: 0
E/flutter ( 4484): #0 List.[] (dart:core-patch/growable_array.dart:264:36)
E/flutter ( 4484): #1 _RouteState.airtableFind (package:example/main.dart:1473:42)
E/flutter ( 4484): <asynchronous suspension>
E/flutter ( 4484):

And that's to be expected, since I'm deliberately entering the wrong username. But I want to get not only a list of errors in the console, but also a dialog box. Why doesn't it appear?

The dialog box call method is correct. I put it in the part of the code that fires when the username is valid. It appears exactly as intended.

But why doesn't this method work in this part of the code?

on DioError catch (e) {
      // TODO: Error handling
      if (e.response != null) {
        // print(e.response.data);
        print(e);
        showInvalidUsernameDialog(context);

      } else {
        // print(e.request);
        print(e.message);
        showInvalidUsernameDialog(context);
      }

And how can I make this dialog appear in case of an error?

Edit 1. _RouteState.airtableFind (package:example/main.dart:1473:42) is referring to idString = response.data['records'][0]['id'];. This happens when my user enters their login incorrectly.

CodePudding user response:

Use catch directly and check if its of type DioError. This is a known behaviour.. Its not catching 400 or 500 errors

https://github.com/flutterchina/dio/issues/1198

try{

}catch(e){
 print(e.toString()); 
}

or

try{
}catch(e){
 if(e is DioError)
 {
 }
}
  • Related