scientific theories
scientific theories

Reputation: 19

This expression has a type of 'void' so its value can't be used

I have created a new method to use it at as recursion i am creating function to get refresh token backe from API server

  void postLogin(LoginRequest loginRequest) async {
    _postStream.sink.add(Result<String>.loading("Loading"));
    try {
      final response = await client.post('$BASE_URL\/$LOGIN_URL', headers: customHeader, body: json.encode(loginRequest));
      print(response.body.toString());
      if (response.statusCode == 200) {
        _postStream.sink.add(Result<LoginResponse>.success(
            LoginResponse.fromJson(json.decode(response.body))));
      } else if(response.statusCode == 401) {
           getAuthUpadate(postLogin(loginRequest));//error is showing at this place
      }
      else{
        _postStream.sink.add(Result.error(REQUEST_ERROR));

      }
    } catch (error) {
      print(error.toString());
      _postStream.sink.add(Result.error(INTERNET_ERROR));
    }
  }
//////////////

i tried this function inside function in flutter it saying void cant be allowed

  getAuthUpadate(Function function()) async {
    try{
      final response = await client.post('$BASE_URL\/NEW_ACCESS_TOKEN_PATH', headers: customHeader,body:json.encode('RefreshToken'));
      //get the refreshToken from response
      String accessToken = response.body.toString();
      //store this access token to the data
      function();
    }
    catch (error){

    }
  }

Upvotes: 0

Views: 1704

Answers (2)

Ben Butterworth
Ben Butterworth

Reputation: 28828

This error happened to me when I passed in a void in a callback method (e.g. calling function() when function is declared to be void function() { ... }.

For example:

IconButton(
        icon: Icon(
          MdiIcons.plusCircle,
        ),
        onPressed: _navigateToJoinQuizScreen()); // <--- Wrong, should just be _navigateToJoinQuizScreen

Upvotes: 1

Shri Hari L
Shri Hari L

Reputation: 4913

I could give you an idea about this issue.
You are just calling the function which has return value void and passing it as a parameter to the function getAuthUpdate.

You can try something like this,

getAuthUpadate(Function function, LoginRequest loginRequest) async { } 

and in the postLogin method, you could try something like this,

else if(response.statusCode == 401) {
     getAuthUpadate(postLogin, loginRequest);  // Just pass Reference as parameter! 
}

finally, the iteration part of getAuthUpdate method,

 try{
      final response = await client.post('$BASE_URL\/NEW_ACCESS_TOKEN_PATH', headers: customHeader,body:json.encode('RefreshToken'));
      //get the refreshToken from response
      String accessToken = response.body.toString();
      //store this access token to the data
      function(loginRequest);
  }
  catch (error){

  }

Hope that works!

Upvotes: 1

Related Questions