How to manually trigger AsyncSnapshot error with FutureBuilder

412

Return

return Future.error("Error Info", StackTrace.fromString("StackTrace Error message"));
Share:
412
faithomotoso
Author by

faithomotoso

Updated on December 20, 2022

Comments

  • faithomotoso
    faithomotoso over 1 year

    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.