【问题标题】:flutter - Avoiding whole widget rebuild颤振 - 避免整个小部件重建
【发布时间】:2020-10-18 05:41:19
【问题描述】:

在 Future 构建器中尝试“点赞按钮”以及许多其他小部件,如下所示,

onPressed: () {
    if (aleadyLiked.length > 0) {
        unlike(profileId);
     } else {
        like(profileId);
     }
   setState(() {});
 },

这就是我未来的建设者开始的方式,

@override
Widget build(BuildContext context) {
return FutureBuilder(
  future: getProfile(profileId), 
  builder: (context, snapshot) {
  =======Other widgets here======
  }

问题是 onPressed of like icon-button 我正在执行 setState() 导致整个 Future 构建器重新加载,有没有办法更新 Like Button 和 Like 计数,我正在考虑使用一些客户端计数器逻辑来回调实际的数据库更新。请帮助。

可以在initState()上加载配置文件部分,但是如何处理更新和反映“喜欢”,可以单独重新加载喜欢按钮区域吗?

【问题讨论】:

  • 这能回答你的问题吗? How to deal with unwanted widget build?
  • 不,这基本上解决/指示在 initState() 上加载配置文件,这很好,我的问题是,在配置文件加载后如何处理“喜欢”更新,可以那个 Like-button 区域单独重新加载?

标签: flutter dart google-cloud-firestore async-await widget


【解决方案1】:

您不应该像这样获取此用户配置文件,但您可以做的是,您可以在 initState 中获取用户配置文件,并且在未加载数据之前,您可以显示任何加载程序。

这样的……

  User _user;
  
  Future<User> getUserProfile(profileId) async{
    ///Todo : Implementation of get User Profile
  }
  
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getUserProfile("userId").then((user){
      setState(() {
        _user =user;
      });
    })
  }

@override
  Widget build(BuildContext context) {
    return Scaffold(
      body: this._user == null ? CircularProgressIndicator() : UserWidget(),
    );
  }

【讨论】:

  • 配置文件部分,是的,我可以在 initState() 中加载它,但是更新 'Likes' 呢,这需要完整的小部件加载吗?
  • 我使用了你的答案,谢谢,但请检查我的答案是如何完全实现的
【解决方案2】:

这就是我最终实现它的方式,

void initState() {
 getProfile(profileId).then((user){
  setState(() {
    _profile =user;
    _counter =_profile.profilelikes.length;
    _isAlreadyLiked=_profile.allreadyliked.length > 0;
  });
 });
 super.initState();
}

而 OnPressed() 是

                                                            onPressed: () {
                                                              if (_isAlreadyLiked) {
                                                                unlike(profileId);
                                                                setState(() {
                                                                  _counter--;
                                                                  _isAlreadyLiked=false;
                                                                });
                                                              } else {
                                                                like(profileId);
                                                                setState(() {
                                                                  _counter++;
                                                                  _isAlreadyLiked=true;
                                                                });
                                                              }
                                                            },

缺点:其他用户的点赞只会反映在 Wiget 重新加载上,这对我来说很好,目前。

【讨论】:

    猜你喜欢
    • 2021-04-03
    • 1970-01-01
    • 2020-09-07
    • 2019-01-14
    • 2019-11-02
    • 1970-01-01
    • 2020-12-15
    • 2021-03-18
    • 1970-01-01
    相关资源
    最近更新 更多