flutter 抖动中的空HTTP响应

ar5n3qh5  于 2023-05-29  发布在  Flutter
关注(0)|答案(1)|浏览(193)

我正在开发一个移动的应用程序,在后端使用flutter和laravel,我做了一个API来添加新记录,当我用Thunder客户端测试api时,它工作得很好,但是当我从flutter调用它时,我得到了null响应。
this is how the error looks lik

final _messengerState = scaffoldKey.currentState; // this variable is for the snackbar
scanParticipant() async {
    Map data = {
      'event': 1,
      'participant': 11,
      'recorder': 6,
    };
    print(data);

    Response response = await _crud.postRequest(addParticipantRoute, data);
    ...
  }
postRequest(String url, Map data) async {
    try {
      var response = await http.post(Uri.parse(url),
          headers: {"Accept": "application/json"}, body: data);
      return response;
    } catch (e) {
      print('Error catch $e');
    }
  }

和后端

public function addRecord(Request $request)
    {

        $fields = $request->validate([
            'event' => 'required|integer',
            'participant' => 'required|integer',
            'recorder' => 'required|integer'
        ]);

        $user = Record::where('event',$fields['event'])->where('participant',$fields['participant'])->first();
        
        if($user == null) 
        {
            Record::create([
                'event' => $fields['event'],
                'participant' => $fields['participant'],
                'recorder' => $fields['recorder'],
                'moment' => Carbon::now()->addHour(),
            ]);

            return response([
                'message' => 'success',
            ],201);
        }else {
            return response([
                'message' => 'already scanned'
            ],422);
        }
        
    }

注意:我在尝试此功能之前登录

63lcw9qa

63lcw9qa1#

我后来解决了这个问题。问题出在数据变量上,我应该把所有的字段转换成字符串,然后发送它。

Map data = {
  'event': '1',
  'participant': '11',
  'recorder': '6',
};

相关问题