如何在不使用 GlobalKey.currentState 的情况下平滑更新 Flutter AnimatedList?

2024-01-05

我发现的将项目插入 Flutter AnimatedList 的所有示例都利用 GlobalKey.currentState 告诉 AnimatedList 小部件已插入项目。从列表中删除也是如此。

众所周知,在 Flutter 中 GlobalKey 的 currentState 可能为 null。

就我而言,我正在等待 Firebase 消息的发送。当消息看起来被插入到 AnimatedList 的列表数据中时,如果遵循 AnimatedList 所示的流行示例,则将使用分配的 GlobalKey.currentState 告诉 AnimatedList 新项目插入的位置。

但是,就我而言,当我尝试使用它时,GlobalKey.currentState 为空。

否则我还能用什么?我知道我可以使用 setState,但这会导致完整的小部件刷新,在这种情况下,它显示为屏幕上的闪烁,而不是所需的 AnimatedList 插入,这是很好且平滑的。

许多人都避免使用 InheritedWidget,但我无法理解如果这是正确的技术,在这种情况下将如何实现它。

/////////
// NOTE:  This is close to real code, but consider it pseudocode since it is a cut and paste from the real code less quite a bit of extra  stuff I have in there.  It certainly will not compile but it is representative of the issue.
///////// 

class Conversation extends StatefulWidget {
  Conversation({Key key, @required this.cid, this.notificationsHub}) : super(key: key);

  // A bit of a hack here, but my Firebase cloud messages come through this object
  MyNotificationsHub notificationsHub;

  // a conversation id 
  final int cid;

  @override
  ConversationState createState() => ConversationState(cid: this.cid, notificationsHub: notificationsHub);
}

//////////

class ConversationState extends State<Conversation> 
    with MyNotificationsListener {
       ConversationState({Key key, @required this.cid, this.notificationsHub}); // : super(key: key);

MyNotificationsHub notificationsHub;

List<ConversationFeed> feed = List();

// The GlobalKey keeps track of the visible state of the list items
// while they are being animated.
final GlobalKey<AnimatedListState> _listKey = GlobalKey();

///////////

initState() {
    notificationsHub.addNotificationsListener(this);
}

///////////
// This is where the issue occurs, when a Firebase message comes in and I attempt to use the GlobalKey the currentState component is null
///////////
void receiveAlert ( MyNotificationItem alert ){
  int insertIndex=0;

  feed.insert(insertIndex, new ConversationFeed( alert ) );

  //******* Issue below, null currentState *******
  _listKey.currentState.insertItem(insertIndex);
  //******* Issue above, null currentState *******
}

////////////

 @override
  Widget build(BuildContext context) {
     return Scaffold(
        appBar: AppBar(
          title: Text("Conversation")),
        body: 

          new AnimatedList(

            // Give the Animated list the global key
            key: _listKey,

            initialItemCount: feed.length,

            itemBuilder: (context, index, animation) {
            return ListTile(
                title: Text ( feed[index].getMessageContentsString() );
          })
       );
}

实际结果是运行时由于GlobalKey的currentState为null而抛出空指针错误。

/// 更新:

我尝试使用 AnimatedList.of(context) 方法和 Builder 来解决这个问题。我按照以下方式尝试过:

// Declared an instance variable of BuildContext in my class definition

BuildContext acontext;

// in the Builder call I assigned it in the following manner:

body: Builder(
            builder: (BuildContext zcontext) { 
              acontext = zcontext ;
              return ( AnimatedList (....) ); }
            )

// & inside my receiveAlert method I used it in the following manner:

void receiveAlert ( MyNotificationItem alert ){
 ....
 AnimatedList.of(acontext).insertItem(insertIndex);
}

当使用上述结构时,我收到以下错误:

使用不包含 AnimatedList.of() 的上下文调用 动画列表。

我确信 Flutter 团队已经提供了一种触发 Widget 树外部更新的方法,我只是不确定如何实现它。也许我的技术不正确。


在动画列表小部件中以动画方式引入新项目时,我遇到了同样的问题。看起来很奇怪,您可以通过检查状态是否仍然为空来解决这个问题:

if(animatedListKey.currentState != null)
  animatedListKey.currentState.insertItem(0);

这意味着在重建时它不会尝试插入项目,直到状态可用。尝试一下。

您可以在此处查看我的教程中的代码:https://github.com/Morthor/flutter_todo_app_talk/blob/youtube_animatedList/lib/main.dart https://github.com/Morthor/flutter_todo_app_talk/blob/youtube_animatedList/lib/main.dart

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在不使用 GlobalKey.currentState 的情况下平滑更新 Flutter AnimatedList? 的相关文章

随机推荐

  • PgAdmin 的 Kubernetes 持久卷挂载

    我正在尝试为我的 pgadmin 部署创建持久卷声明 以便在每次从 CD 管道部署后推出更新时可以保留我的设置 服务器等 在我的日志中 我收到以下错误 2020 10 05 00 54 56 0000 91 INFO Worker exit
  • Angular2:如何向应用程序注入没有装饰器的服务?

    当尝试使用时Http在 App 组件 我们正在引导的组件 中一切正常 找到 export default class AppComponent constructor public http Http console log this ht
  • 删除 MagicalRecord 中的实体不会持久

    我在使用 MagicalRecord 时遇到了一个奇怪的问题 删除不会持久 当我删除时 NSFetchedResultsControllerDelegate 正确地看到该对象已被删除 但是 如果我关闭并重新打开应用程序 该实体就会重新出现
  • Executor 上的 AsyncTask 和 PriorityBlockingQueue 实现问题

    在浪潮中这个问题 https stackoverflow com questions 12039596 asynctask on executor and priorityblockingqueue并有许多提示另一个 https stack
  • 测量计时器的精度(例如秒表/QueryPerformanceCounter)

    鉴于StopwatchC 中的类可以在下面使用三个不同的计时器 例如 系统定时器例如精度约 10 ms取决于可以设置的定时器分辨率timeBeginPeriod https msdn microsoft com en us library
  • plot.new尚未被调用

    为什么会发生这种情况 plot x y yx lm lt lm y x lines x predict yx lm col red 错误于plot xy xy coords x y type type plot new尚未被调用 某些操作
  • C# 解码(解压缩)PDF 文件的 Deflate 数据

    我想在 C 中解压缩一些 DeflateCoded 数据 提取的 PDF 不幸的是 我每次都会遇到异常 解码时发现无效数据 但数据是有效的 private void Decompress FileStream fs new FileStre
  • Number::toString() 抽象操作

    我试图理解如何数字 toString x https 262 ecma international org 11 0 sec numeric types number tostring抽象操作有效 据我所知 其要点似乎是非常大 n gt 2
  • exec 总是返回 -1 (或 127)

    我在生产服务器上使用 php 5 2 9 并且 exec 函数的行为似乎是 非标准 如果我跑exec ls output return var then output将按预期包含当前文件夹中的文件列表 但是 return var将被设置为
  • Google Spanner:JDBC 连接字符串?

    虽然 Spanner 看起来令人兴奋 但 Simba JDBC 驱动程序的文档 包含在此处的下载链接中 https cloud google com spanner docs partners drivers https cloud goo
  • 如何确保 slurm 中的 python 提交脚本位于发出 sbatch 命令的位置?

    我有一个运行的 python 提交脚本sbatch using slurm sbatch batch py 当我这样做时 事情无法正常工作 因为我认为 batch py进程没有继承正确的环境变量 因此 而不是运行batch py从哪里sba
  • 使用 Ruby 的 TracePoint 获取方法参数

    我可以使用 TracePoint API 访问 Ruby 方法的参数 def foo foo arg end trace TracePoint trace call c call do tp tp disable case tp metho
  • 在赋值 A(:) = B 中,A 和 B 中的元素数量必须相同

    例如 当尝试运行我的代码时 for ii 1 10 output ii rand 3 end 我收到错误 In an assignment A B the number of elements in A and B must be the
  • 目标 C:SHA1

    如何在 Objective c 中 sha1 一个字符串或一组数字 CommonCrypto Apple 框架 具有计算 SHA 1 哈希值的函数 包括一步哈希值 include
  • 给定 x、y 和色调,获取 HSL 值

    给定一个看起来像这样的颜色选择器 我正在尝试根据光标的 x 和 y 位置以及右侧滑块的色调来计算 HSL 值 我的数学技能相当薄弱 尽管我所拥有的很接近 但尝试获取真正浅的全强度颜色很麻烦 最终会变得太灰 这是我当前使用的功能 getHSL
  • C# 中使用反射进行方法拦截

    我编写了一个抽象类 它使用反射来查找构造函数中标有属性的字段 如下所示 AttributeUsage AttributeTargets Field public class TrackedField Attribute public cla
  • 定义频率上的音量 (C#)

    我不明白为什么音量定义的频率不符合预期 我依次将声音以几个指定的频率发送到麦克风 然后我对麦克风缓冲区进行 FFT 在 FFT 退出时 我有一个复数数组 要了解定义频率上的声音音量 我查看我的数组 该数组中的元素数量是这样得到的 MyFre
  • 使用 Java 在线程之间传输数据

    我正在编写一个模仿电影院的多线程应用程序 每个参与的人都是自己的线程 并发必须完全由信号量来完成 我遇到的唯一问题是如何基本上链接线程以便它们可以通信 例如通过管道 例如 Customer 1 是一个线程 它获取一个信号量 让它走到票房 现
  • VBA:使用两个参数调用 SQL Server 存储过程

    正如标题中提到的 我只想从 VBA 调用 SQL Server 存储过程 我可以这样调用我的存储过程 EXEC dbo spClientXLS Nr 131783 date 21 09 2014 Nr is a varChar 50 输入值
  • 如何在不使用 GlobalKey.currentState 的情况下平滑更新 Flutter AnimatedList?

    我发现的将项目插入 Flutter AnimatedList 的所有示例都利用 GlobalKey currentState 告诉 AnimatedList 小部件已插入项目 从列表中删除也是如此 众所周知 在 Flutter 中 Glob