0

i am developing an app and it has a page; users can write comments. what i want; users added their comments after listview should be refreshed.

Added my addComment function.

MyController

  var isLoading = true.obs;
  var addModel = AddCommentModel().obs();

  AddCommentController(this.comment, this.userUnique, this.articleID);

  @override
  void onInit() {
    super.onInit();
  }

  void addComment() async {
    try {
      isLoading(true);
      var response =
          await ApiService.addComment(comment, userUnique, articleID);
      if (response != null) {}
    } finally {
      isLoading(false);
    }
  }
}

My view;

body: Column(
        children: <Widget>[
          Expanded(
            child: Obx(() {
              if (commentsController.isLoading.value) {
                return Center(
                  child: SpinKitChasingDots(
                      color: Colors.deepPurple[600], size: 40),
                );
              }
              return ListView.builder(
                controller: scrollController,
.
.
.
onPressed: () async {
                                  Future.delayed(Duration(seconds: 1))
                                      .then((_) {
                                    AddCommentController(parser.unemojify(textFieldCtrl.text), box.read('uid'),
        box.read('rid'))
    .addComment();
jancooth
  • 555
  • 1
  • 10
  • 25

2 Answers2

0
  1. Add the part where you use addComment function

  2. If you are calling addComment function somewhere else than the Class where you call data use Get.find() to get you controller(I may have made mistake)

0

I think what you are doing here is not good practice. What you could have done to achieve the same thing in some straightforward way. I mean list are automatically reactive in nature in rx world. So you could do something like:

In controller:

final commentList=<Comment>[].obs;

Future<void> addComment()async{
  final response =await ApiService.addComment(params);
  commentList.add(response);
  }

And in your View:

Obx(()=>ListView.builder(
          itemCount: controller.commentList.length,
          itemBuilder:......
  ),),
S. M. JAHANGIR
  • 4,324
  • 1
  • 10
  • 30