I am a newbie in fetching data from APIs
I have an api endpoint containing two query params and also having a body of string and int. I want to make an already existing post to be featured on the home page with time limit.
in my repo I have written this
class FeaturePost {
Future featurePost(int duration, String period) async {
SharedPreferences preferences = await SharedPreferences.getInstance();
var token = preferences.getString('token').toString();
final queryParameters = {
'postpId': 'postId',
'id': 'id',
};
http.post(Uri.dataFromString("https/url.com/api/v1/post?" parameters: queryParameters),
headers: {
'Content-Type': 'application/json',
'x-access-token': token,
},
body: {
"duration": duration,
"period": period
});
here's my button
GestureDetector(
onTap: () async {
setState(() {
period =
"${dropdownValue}";
});
if (_postKey
.currentState!
.validate()) {
var create =
PostModel(
postId:
widget.postId,
id: widget.userId,
period: period,
duration: int.parse(
durationController
.text),
);
createPost
.newPostDuration(
create,
widget.postId,
);
}
},
child: Center(
child: Text('Make Post Featured',),
),
)
my controller
class FeaturePostController extends GetxController {
final featureAPostRepo = FeaturePost();
Future<dynamic> newFeaturedPost(ostpId, id) async {
try {
final result = await featureAnAdRepo.featureAnAd(postId, id);
Get.back();
await fromFeaturedAds.fetchFeaturedAds();
// Get.snackBar('Success', 'Post Featured Successfully');
print(result);
return result;
} catch (e) {
throw Exception(e);
}
}
}
this is the response i get Unhandled Exception: Null check operator used on a null value.
CodePudding user response:
Since you're getting Null check operator used on a null value exception, it's probably because you haven't assigned your _postKey to the form that you are trying to validate. Make sure you're wrapping all your fields inside a form and that your key is assigned to the key parameter of that form
CodePudding user response:
I was able to fix it. It was because I wasn't parsing all required params properly. All I had to do was add "postId" and "Id" like this ```featurePost(String postId, String Id, int duration, String period) and some rearrangements and it worked