- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我发现的将项目插入 Flutter AnimatedList 的所有示例都利用 GlobalKey.currentState 告诉 AnimatedList 小部件已插入项目。从列表中删除也是如此。
众所周知,在 Flutter 中 GlobalKey 的 currentState 可能为 null。
就我而言,我正在等待 Firebase 消息的发送。当消息看起来被插入到 AnimatedList 的列表数据中时,如果遵循 AnimatedList 所示的流行示例,则可以使用分配的 GlobalKey.currentState 告诉 AnimatedList 新项目插入的位置。
但是,就我而言,当我尝试使用它时,GlobalKey.currentState 为 null。
否则我还能用什么?我知道我可以使用 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() called with a context that does not contain an AnimatedList.
我确信 Flutter 团队已经提供了一种触发 Widget 树外部更新的方法,我只是不确定如何实现它。也许我的技术不正确。
最佳答案
在动画列表小部件中以动画形式引入新项目时,我也遇到了同样的问题。看起来很奇怪,您可以通过检查状态是否仍然为空来解决这个问题:
if(animatedListKey.currentState != null)
animatedListKey.currentState.insertItem(0);
这意味着它不会在重建时尝试插入项目,直到状态可用。尝试一下。
您可以在此处查看我的教程中的代码:https://github.com/Morthor/flutter_todo_app_talk/blob/youtube_animatedList/lib/main.dart
关于flutter - 如何在不使用 GlobalKey.currentState 的情况下平滑更新 Flutter AnimatedList?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56896814/
我是 Java 新手,这是我的代码, if( a.name == b.name && a.displayname == b.displayname && a.linknam
在下面的场景中,我有一个 bool 值。根据结果,我调用完全相同的函数,唯一的区别是参数的数量。 var myBoolean = ... if (myBoolean) { retrieve
我是一名研究 C++ 的 C 开发人员: 我是否正确理解如果我抛出异常然后堆栈将展开直到找到第一个异常处理程序?是否可以在不展开的情况下在任何 throw 上打开调试器(即不离开声明它的范围或任何更高
在修复庞大代码库中的错误时,我观察到一个奇怪的情况,其中引用的动态类型从原始 Derived 类型更改为 Base 类型!我提供了最少的代码来解释问题: struct Base { // some
我正在尝试用 C# 扩展给定的代码,但由于缺乏编程经验,我有点陷入困境。 使用 Visual Studio 社区,我尝试通过控制台读出 CPU 核心温度。该代码使用开关/外壳来查找传感器的特定名称(即
这可能是一个哲学问题。 假设您正在向页面发出 AJAX 请求(这是使用 Prototype): new Ajax.Request('target.asp', { method:"post", pa
我有以下 HTML 代码,我无法在所有浏览器中正常工作: 我试图在移动到
我对 Swift 很陌生。我如何从 addPin 函数中检索注释并能够在我的 addLocation 操作 (buttonPressed) 中使用它。我正在尝试使用压力触摸在 map 上添加图钉,在两
我设置了一个详细 View ,我是否有几个 Nib 文件根据在 Root View Controller 的表中选择的项目来加载。 我发现,对于 Nibs 的类,永远不会调用 viewDidUnloa
我需要动态访问 json 文件并使用以下代码。在本例中,“bpicsel”和“temp”是变量。最终结果类似于“data[0].extit1” var title="data["+bpicsel+"]
我需要使用第三方 WCF 服务。我已经在我的证书存储中配置了所需的证书,但是在调用 WCF 服务时出现以下异常。 向 https://XXXX.com/AHSharedServices/Custome
在几个 SO 答案(1、2)中,建议如果存在冲突则不应触发 INSERT 触发器,ON CONFLICT DO NOTHING 在触发语句中。也许我理解错了,但在我的实验中似乎并非如此。 这是我的 S
如果进行修改,则会给出org.hibernate.NonUniqueObjectException。在我的 BidderBO 类(class)中 @Override @Transactional(pr
我使用 indexOf() 方法来精细地查找数组中的对象。 直到此刻我查了一些资料,发现代码应该无法正常工作。 我在reducer中尝试了上面的代码,它成功了 let tmp = state.find
假设我有以下表格: CREATE TABLE Game ( GameID INT UNSIGNED NOT NULL, GameType TINYINT UNSIGNED NOT NU
代码: Alamofire.request(URL(string: imageUrl)!).downloadProgress(closure: { (progress) in
我是一名优秀的程序员,十分优秀!