Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manually trigger AsyncSnapshot error with FutureBuilder

I'm tring to manually trigger snapshot.hasError when a future is called and a response is gotten. For instance I have a future

   Future<dynamic> getJson() {
     Dio dio = Dio();
     var response = dio.get("https://www.jsononline.com/posts?id=9999");
     if (response.statusCode == 200) {
       return response;
     } else {
      // return AsyncSnapshot error
      }
    }

I've tried return AsyncSnapshot.withError(ConnectionState.done, "An error occurred"); but in the FutureBuilder snapshot.hasError is still null, instead it goes to snapshot.data.

I want to be able to trigger the error if I get a 404 message for instance.

like image 894
faithomotoso Avatar asked Sep 21 '25 06:09

faithomotoso


1 Answers

Return

return Future.error("Error Info", StackTrace.fromString("StackTrace Error message"));
like image 137
Jitesh Mohite Avatar answered Sep 22 '25 18:09

Jitesh Mohite