this the function for API in laravel for search:

   public function search(Request $request) {
        try {
            $request->validate(['search' => ['max:15', 'required']]);
            $search = $request->input('search');
            $answerSearch = Answer::where('title','like', '%'.$search.'%')->get();
            return AnswersResource::collection($answerSearch);

        }catch(\Exception $e){
            return response()->json(['status'=>false, 'msg'=> $e->getMessage()], 500);
        }
    }
}

in postman it works like a charm and expected

but with the flutter this is the code to work with this API:

  Future<List<AnswerData>> searchAnswers({required String search}) async {
    final response = await post(
    Uri.parse("$_url/search"),
    headers:  <String, String>{
      'Content-Type': 'application/json; charset=UTF-8',
    },
    body: jsonEncode(<String, String>{"search" : search})
    );
    final answers = answerFromJson(response.body);
    print("fdfsdfdsfdsf ${answers.data}");
    return answers.data;
  }
}

its return null ?


Solution 1: Rohit Krishna

Check the response status by printing 'response.statusCode'.

print('Response status: ${response.statusCode}');