Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to return catch exception in flutter

I working on error handling of api’s. i want if api is crashed then it display a message of "Server is down" something like this, in UI.

I created a class where i’m creating methods of api, here in getBooks method if i modify the api url then it is printing this Exception, and i want it in UI. The problem is getBooks return type is List<Book>> so we can’t return this Exception, any solution how to do this?

Exception
E/flutter (12924): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Exception

here is my api code

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class BooksApi {
  static Future<List<Book>> getBooks(String query) async {
    try {
      final url = Uri.parse(
          'https://gist.githubusercontent.com/JohannesMilke/d53fbbe9a1b7e7ca2645db13b995dc6f/raw/eace0e20f86cdde3352b2d92f699b6e9dedd8c70/books.json');
      final response = await http.get(url);

      if (response.statusCode == 200) {
        final List books = json.decode(response.body);

        return books.map((json) => Book.fromJson(json)).where((book) {
          final titleLower = book.title.toLowerCase();
          final authorLower = book.author.toLowerCase();
          final searchLower = query.toLowerCase();

          return titleLower.contains(searchLower) ||
              authorLower.contains(searchLower);
        }).toList();
      } else {
        throw Exception;
      }
    } catch (e) {
      print("e");
      print(e);
      
       
    }
  
    throw Exception;
  }
}

and calling it like

Future init() async {
    setState(() {
      isLoading = true;
    });
    var books = await BooksApi.getBooks(query); //this
    
    var response = await obj.getProduct();
    print(response);
    setState(() => this.books = books);
    setState(() {
      isLoading = false;
    });
  }

>Solution :

You could handle errors with then and onError :

await BooksApi.getBooks(query).then((books) async {
  setState(() => {
    this.books = books;
    this.isLoading = false;
  })
}, onError: (error) {
  // do something with error
});

or a simple try-catch (you can write try-catch clauses the same way you would in synchronous code).

See handling errors.

You can also use catchError id you don’t use async/await :

BooksApi.getBooks(query).then((books) { 
  setState(() => {
    this.books = books;
    this.isLoading = false;
  })
}).catchError((error, stackTrace) {
  print("error is: $error");
});

See futures error handling.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading