skip to Main Content

I have below 2 methods in my Utility class: One for checking the network connection and on for showing the progress dialog.

static Future<bool> isInternet() async
  {
    bool result = await InternetConnectionChecker().hasConnection;
    return result;
  }

  static ShowProgress(BuildContext context) {
    return showDialog(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return Center(
            child: SpinKitRotatingCircle(color: const Color(0xff2799fa), size: 60.0, ),
          );
        });
  }

I need to apply it on the below API call section:

class _UserDetailsState extends State<UserDetails> {
  ProfileResponse? profileResponse;
  Future<void> fetchData() async {
    try {
      //Utility.ShowProgress(context);
      final response = await getProfileDetails();
      if (response.statusCode == 200) {
        Map<String, dynamic> parsedData = jsonDecode(response.body);
        setState(() {
          profileResponse = ProfileResponse.fromJson(parsedData);
        });
      } else {
        // setState(() {
        //   _data = 'Error: ${response.statusCode}';
        // });
      }
    } catch (error) {
      // setState(() {
      //   _data = 'Error: $error';
      // });
    }
    //Navigator.of(context).pop();
  }

  @override
  void initState() {
    fetchData();
  }
  
  @override
Widget build(BuildContext context) {
return Container(
     )
}
  
  Future<http.Response> getProfileDetails() async{
    if(await Utility.isInternet()){
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    //reading values from SharedPreferences and calling the API
    final String url = My API Path;
    final response = await http.get(Uri.parse(url));
    return response;
    }
    else{
      //show no network alert
    }
}

I added ProgressDialog in the fetchData(), like the commented code in the code, but it is not showing in the UI when calling the API.

Also I have added the network check on the getProfileDetails(), but it is showing below error.

The body might complete normally, causing ‘null’ to be returned, but the return type, ‘FutureOr’, is a potentially non-nullable type. Try adding either a return or a throw statement at the end.

How can I fix these 2 issues?

2

Answers


  1. Chosen as BEST ANSWER

    The fixed code with progress dialog and network check adding below:

    Future<http.Response?> getProfileDetails() async
    {
        if(await Utility.isInternet())
        {
          Utility.ShowProgress(context);
          //reading values from SharedPreferences and calling the API
          final String url = My API Path;
          final response = await http.get(Uri.parse(url));
          Navigator.of(context).pop();
          return response;
        }
        else
        {
          Utility.showToastMessage("No internet connection!",Toast.LENGTH_SHORT,
              ToastGravity.CENTER, Colors.white, 16.0 );
          return null;
        }
    }
    

  2. Whenever you define a method that has some sort of expected output, your code should return the exact same type you defined or an error.

    In your code you have declared a Future like this:

     Future<http.Response> getProfileDetails() async{
    

    You should always return a Future<http.Response>.
    However, when the first If condition is not True, there is nothing to be returned, resulting in your error.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search