Zemichael Muluken
Zemichael Muluken

Reputation: 11

The body might complete normally, causing 'null' to be returned, but the return type, 'FutureOr<DataModel>', is a potentially non-nullable type

    Future<DataModel> submitData(
    int id,
    String user,
    String userId,
    String title,
    String description,
    String location,
    String jobType,
    int category,
    String salary,
    String companyName,
    String url,
    DateTime lastDate,
    bool isPublished,
    bool isClosed,
    DateTime timestamp) async {
 var response = await http.post(Uri.https('jobspace-isnp.herokuapp.com', 'api/jobs'), 
    body: {
    "id": id,
    "user": user,
    "user_id": userId,
    "title": title,
    "description": description,
    "location": location,
    "job_type": jobType,
    "category": category,
    "salary": salary,
    "company_name": companyName,
    "url": url,
    "last_date": lastDate,
    "is_published": isPublished,
    "is_closed": isClosed,
   });
  var data = response.body;
  print(data);

  if (response.statusCode == 201) {
    String responseString = response.body;
    dataModelFromJson(responseString);
  } else
    return null;
}

I am getting this error: The body might complete normally, causing 'null' to be returned, but the return type, 'FutureOr', is a potentially non-nullable type. Try adding either a return or a throw statement at the end.

How can I solve it?

Upvotes: 0

Views: 409

Answers (1)

GNassro
GNassro

Reputation: 1071

Using null safety in your code, can submitData function return null value. to do that just add ? after DataModel so your code will be like this

  Future<DataModel?> submitData(
    int id,
    String user,
    String userId,
    String title,
    String description,
    String location,
    String jobType,
    int category,
    String salary,
    String companyName,
    String url,
    DateTime lastDate,
    bool isPublished,
    bool isClosed,
    DateTime timestamp) async {
 var response = await http.post(Uri.https('jobspace-isnp.herokuapp.com', 'api/jobs'), 
    body: {
    "id": id,
    "user": user,
    "user_id": userId,
    "title": title,
    "description": description,
    "location": location,
    "job_type": jobType,
    "category": category,
    "salary": salary,
    "company_name": companyName,
    "url": url,
    "last_date": lastDate,
    "is_published": isPublished,
    "is_closed": isClosed,
   });
  var data = response.body;
  print(data);

  if (response.statusCode == 201) {
    String responseString = response.body;
    dataModelFromJson(responseString);
  } else
    return null;
}

read more about null safety

Upvotes: 0

Related Questions