Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在颤振中使用socket.io数据更新listview?_List_Flutter_Dart_Socket.io_Setstate - Fatal编程技术网

无法在颤振中使用socket.io数据更新listview?

无法在颤振中使用socket.io数据更新listview?,list,flutter,dart,socket.io,setstate,List,Flutter,Dart,Socket.io,Setstate,我正在使用node.js和express和MongoDB和socket.io开发后端,在前端,我使用flatter和dart 我正在使用emit将数据从后端发送到flatter应用程序,它工作正常,我可以接收消息,因此问题不在后端 当我收到该消息时,我想将其添加到列表中,然后设置state以刷新生成方法以重新生成生成方法,但失败并给出错误: [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: setState() c

我正在使用
node.js
express
MongoDB
socket.io
开发后端,在前端,我使用
flatter
dart

我正在使用
emit
将数据从后端发送到
flatter
应用程序,它工作正常,我可以接收消息,因此问题不在后端

当我收到该消息时,我想将其添加到列表中,然后设置state以刷新生成方法以重新生成生成方法,但失败并给出错误:

[ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: setState() called after dispose(): _NewsState#a696c(lifecycle state: defunct, not mounted)
E/flutter (11883): This error happens if you call setState() on a State object for a widget that no longer appears in the widget tree (e.g., whose parent widget no longer includes the widget in its build). This error can occur when code calls setState() from a timer or an animation callback.
E/flutter (11883): The preferred solution is to cancel the timer or stop listening to the animation in the dispose() callback. Another solution is to check the "mounted" property of this object before calling setState() to ensure the object is still in the tree.
E/flutter (11883): This error might indicate a memory leak if setState() is being called because another object is retaining a reference to this State object after it has been removed from the tree. To avoid memory leaks, consider breaking the reference to this object during dispose().
下面是我用来侦听来自后端的事件的代码片段:

Provider.of(context.globalSocket.connect();
Provider.of(context.globalSocket.on('posts'),(数据){
如果(数据['action']=='add'){
设置状态(){
add(data['data']);
});
}
打印('--emit--');
});
我在build方法中使用了它,并且在state类的顶部声明了postData列表

我还在init state方法中使用future函数从后端获取数据,然后设置state使postData等于来自后端的响应数据,这是成功的

代码段:

Future getPosts()异步{
var data=wait PostController().fetchPosts();
如果(数据['success']==true){
设置状态(){
postData=数据['posts'];
});
}
}
在初始状态内:

@覆盖
void initState(){
super.initState();
未来。延迟(Duration.zero,(){
getPosts(上下文);
});
_editingController=TextEditingController();
}
内部构建方法:

ListView.builder(
itemCount:postData.length,
itemBuilder:(上下文,索引)=>PostView(postData[index]),
);
更新

经过测试,我发现这个问题在用户第一次导航到那个屏幕时并没有发生,但在其他时间它失败了


但是如何解决呢?

经过太多的搜索,我发现问题是由颤振本身引起的,因为我使用的是底部导航栏,有四个屏幕,每次我过了一段时间后去它们那里,它们都会回忆起initstate

最后,我通过使用索引堆栈小部件解决了这个问题


谢谢,

经过太多的搜索,我发现问题是由颤振本身造成的,因为我使用了一个底部导航栏,有四个屏幕,每次经过一段时间后我去它们那里,它们都会回忆起initstate

最后,我通过使用索引堆栈小部件解决了这个问题


谢谢,

你能发布你所指的方法的完整代码吗?该类中的完整代码太多代码将不起作用accepted@Bug将
if(数据['action']=='add')
更改为
if(数据['action']=='add'&&isMounted)
你能发布你所指的方法的完整代码吗?该类中的完整代码太多代码将无效accepted@Bug将您的
if(数据['action']=='add')
更改为
if(数据['action']=='add'&&isMounted)