Search code examples
flutterhttphttp-status-code-404

Expected an identifier and Expected to find ')' error


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


Solution

  • 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);
    }}