0

I've internet turned off my phone to trigger a SocketException error. I make my api call and at the top of the stack the error can be seen on my debugger just before the await call. I want to handle this error at the UI layer. And at the bottom of my call stack I see the method getData . My understanding is: I can wrap this in a try/catch and handle the exception. But I can't seem to grab the error.. Here is the method:

Future<String> getData(String query) async {
    try {
      List<Suburb> suburbs = await model.getSuburbs('au', query);

      if (suburbs.length == 1) {
        suburbCode = suburbs[0].code;
        isButtonDisabled = false;
        return suburbs[0].name;
      }
      return showDialog<String>(
          barrierDismissible: false,
          context: context,
          builder: (context) => SimpleDialog(
                backgroundColor: Colors.black,
                title: Text('Please choose your Suburb:'),
                children: suburbs
                    .map((suburb) => SimpleDialogOption(
                        child: Text(suburb.name),
                        onPressed: () {
                          suburbCode = suburb.code;
                          isButtonDisabled = false;

                          Navigator.of(context).pop(suburb.name);
                        }))
                    .toList(),
              ));
    } on SocketException catch (e) {
      print(e);
    }
  }

The line List<Suburb> suburbs = await model.getSuburbs('au', query); is where I make the api call and where the debugger points to. But I can't seem to 'get in to the catch block'

4
  • 1
    on SocketException catch (e) { should work. Try to add this block within getSuburbs() and check. In one of my code, I catch socket exception where the API call is made [so inside getSuburbs()] in your context) and throw a generic exception (via enum) from the caller [i.e.getData()] Commented Apr 17, 2020 at 13:20
  • maybe I should add that the method is a future for a FutureBuilder. But I don't think that makes any difference Commented Apr 17, 2020 at 13:58
  • thanks..getSuburbs() needed an await. Commented Apr 17, 2020 at 14:07
  • Cool. Let me add it as an answer so that others would be benefitted as well. Commented Apr 17, 2020 at 14:15

1 Answer 1

1

The try-catch block will need at the Http post/get call as well. So, just catch the socket exception in getSuburbs(), do the needful and throw a general exception from there, if required. Something like below would work :

Future<string> getSuburbs(string country, string query){
    try {
        http.Response response =
            await http.post(.........
      } on SocketException catch (e) {
        print(e);
        throw Error.noNetworkConnection;
      } catch (e) {
        print(e);
        throw Error.unknown;
      }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.