预期为标识符和预期查找“)”错误

Expected an identifier and Expected to find ')' error

提问人:katamykenji 提问时间:11/16/2023 最后编辑:Bill Hilemankatamykenji 更新时间:11/17/2023 访问量:37

问:

我是初学者编码员,我不明白如何解决这个错误

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

错误: 需要标识符。 预计会找到 ')'

我应该把 IF 放在哪里以检查状态代码?我需要创建新课程吗?

我尝试重新排列代码

颤振 http-status-code-404

评论


答:

0赞 Munsif Ali 11/16/2023 #1

试试这段代码,希望这能解决你的问题。


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

评论

0赞 katamykenji 11/16/2023
它确实解决了错误,但现在它又出现了另一个错误 - [无效的请求正文“{{”bus_stop_id“:”1076“}}”。
0赞 Rosha buha 11/17/2023 #2

请试试这个代码

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