skip to Main Content

I am beginner coder and i dont understand how to resolve this error

Future<http.Response> fetchData(String query) async {
    return  http.post(
      Uri.parse('https://laravelsyd-fypfinalver.herokuapp.com/getBusService'),

      headers: <String, String>{
        'Content-Type': 'application/json; charset=UTF-8',
      },
      body: { 
        jsonEncode({'bus_stop_id': busCode})
      },
        if (response.statusCode == 200) {
          final List<dynamic> data = json.decode(response.body);
          dataList = List<Map<String, dynamic>>.from(data);
          setState(() {// Update the state to trigger a rebuild with the fetched data
             dataList = List<Map<String, dynamic>>.from(data);
          },);
        } else {
          print (response.statusCode);
        }
        );
  }

error:
Expected an identifier.
Expected to find ‘)’

where should i put the if to check the status code? do i need to make new class?

i tried rearranging the code

2

Answers


  1. try this code hope this will solve your problem.

    
    Future<http.Response> fetchData(String query) async {
    final response = await http.post(
          Uri.parse('https://laravelsyd-fypfinalver.herokuapp.com/getBusService'),
    
          headers: <String, String>{
            'Content-Type': 'application/json; charset=UTF-8',
          },
          body: { 
            jsonEncode({'bus_stop_id': busCode})
          },
           
            );
    return response;
      }
    
    
    Login or Signup to reply.
  2. Please try this code

      Future<http.Response> fetchData(String query) async {
    var response = await http.post(
        "https://laravelsyd-fypfinalver.herokuapp.com/getBusService",
        headers: <String, String>{
          'Content-Type': 'application/json; charset=UTF-8',
        },
        body: jsonEncode({'bus_stop_id': busCode}));
    
    if (response.statusCode == 200) {
      final List<dynamic> data = json.decode(response.body);
      dataList = List<Map<String, dynamic>>.from(data);
      setState(
        () {
          // Update the state to trigger a rebuild with the fetched data
          dataList = List<Map<String, dynamic>>.from(data);
        },
      );
    } else {
      print(response.statusCode);
    }}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search