【问题标题】:Flutter ListView.builder's UI not getting updated by getx even if it receives updated data即使收到更新的数据,Flutter ListView.builder 的 UI 也不会被 getx 更新
【发布时间】:2021-12-27 05:53:58
【问题描述】:

我是初学者尝试用颤振学习 getx,这是我的控制器

class QuestionAnswers_Getx extends GetxController {
  final _questionandanswers = <QA>[].obs;
  List<QA> get questionandanswers => _questionandanswers;

  @override
  void onInit() {
    super.onInit();
    getall_questionandanswers();
    Future.delayed(Duration.zero,()=>listen_firebase());
  }


  getall_questionandanswers() async {
    await qaref.get().then((QuerySnapshot query){
      _questionandanswers.addAll(query.docs.map((DocumentSnapshot doc) => doc_to_obj(doc)).toList());
    });
  }

  listen_firebase() async {
    await qaref.snapshots().listen((QuerySnapshot querySnapshot) {
          
          querySnapshot.docChanges.forEach((DocumentChange doc) {         
              if(doc.type==DocumentChangeType.added){
                  _questionandanswers.add(doc_to_obj(doc.doc));          
              }else if(doc.type==DocumentChangeType.modified){
                _questionandanswers[doc.newIndex] = doc_to_obj(doc.doc);
                // print(_questionandanswers[doc.newIndex]);
              }else if(doc.type==DocumentChangeType.removed){
                print("getx--------------removed line");
              }
          });
     });

  }

}

这是我的 Obx 小部件,我想在其中使用控制器的数据。我在前面的页面中使用了 get.put 并在此处找到控制器

final qa_controller = Get.find<QuestionAnswers_Getx>();
Obx(() {
              RxList<QA> myqa = <QA>[].obs;
              myqa.addAll( qa_controller.questionandanswers.where((element)=>element.user_name=="Jaswant patil").toList()   );                
//here if i use print(myqa[0].answer) then it gives me old as well as new data for answer parameter
                  return ListView.builder(
                      scrollDirection: Axis.vertical,   
                      shrinkWrap: true,
                      physics: ScrollPhysics(),
                      itemCount: myqa.length,
                      itemBuilder: (context,index){
                          return QAwidget(
                              answer: myqa[index].answer,
                              comment_count: myqa[index].comment_count,
                              upvotes: myqa[index].upvotes,
                              user_image_url:
                                  "https://cdn.pixabay.com/photo/2020/02/24/06/33/crescent-4875339__340.jpg",
                              user_name: myqa[index].user_name,
                              user_occupation: myqa[index].user_occupation,
                              post_image_url: myqa[index].post_image_url,
                              category: myqa[index].category,
                              hasimage: myqa[index].hasimage,
                              question: myqa[index].question);
                      },

上面的 ListView 在 Ui 中正确显示了初始数据,但即使正在接收更新的数据,也不会在数据更改时显示更新的 UI。但是当我交换页面并再次返回此页面时,会显示更新的数据,我认为需要在数据更改时调用 setState 但不知道放在哪里。

感谢您的回答,请原谅上述问题中出现的任何语法错误

【问题讨论】:

  • QAwidget 有状态吗?
  • No QAwidget 不是有状态的

标签: flutter setstate state-management flutter-getx


【解决方案1】:

首先,尝试使用 variablename.value.addAll 那是行不通的 你可以随时刷新变量,我认为 observables 有一个方法调用 refresh()

【讨论】:

    猜你喜欢
    • 2021-01-22
    • 2020-12-07
    • 2021-12-17
    • 2021-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-25
    相关资源
    最近更新 更多