createTextpost method

Future<void> createTextpost(
  1. BuildContext context, {
  2. String? communityId,
})

Implementation

Future<void> createTextpost(BuildContext context,
    {String? communityId}) async {
  log("createTextpost...");
  bool isCommunity = (communityId != null) ? true : false;
  if (isCommunity) {
    log("in community...");
    await AmitySocialClient.newPostRepository()
        .createPost()
        .targetCommunity(communityId)
        .text(textEditingController.text)
        .post()
        .then((AmityPost post) {
      ///add post to feed
      Provider.of<CommuFeedVM>(context, listen: false).addPostToFeed(post);
      Provider.of<CommuFeedVM>(context, listen: false)
          .scrollcontroller
          .jumpTo(0);
      notifyListeners();
    }).onError((error, stackTrace) async {
      log(error.toString());
      await AmityDialog()
          .showAlertErrorDialog(title: "Error!", message: error.toString());
    });
  } else {
    await AmitySocialClient.newPostRepository()
        .createPost()
        .targetMe() // or targetMe(), targetCommunity(communityId: String)
        .text(textEditingController.text)
        .post()
        .then((AmityPost post) {
      ///add post to feed
      Provider.of<FeedVM>(context, listen: false).addPostToFeed(
        post,
      );
      Provider.of<FeedVM>(context, listen: false).scrollcontroller.jumpTo(0);
      notifyListeners();
    }).onError((error, stackTrace) async {
      log(error.toString());
      await AmityDialog()
          .showAlertErrorDialog(title: "Error!", message: error.toString());
    });
  }
}