我有一个创建StatefulWidget post_view的DataTable。用于填充DataTable的数据来自另一个名为Post的类中的静态方法:
`
static generateData(){
List<Post> postList= [];
postList.add(Post(title: "Coolest Post", numDownVotes: 6, numUpVotes: 9));
postList.add(Post(title: "Covid-19 vaccine found!", numDownVotes: 3, numUpVotes: 67));
postList.add(Post(title: "Unreal ending to basketball game", numDownVotes: 2, numUpVotes: 23));
postList.add(Post(title: "Sample Post", numDownVotes: 2, numUpVotes: 6));
postList.add(Post(title: "What A Save!", numDownVotes: 5, numUpVotes: 34));
return postList;
}`
我在表中有一个DataCells,这样数据单元格中的上/下票数就在一行小部件中,并有一个图标按钮来分别更改或增加/减少向上/向下的票数。因此,我在post_view中声明了一个变量‘post_view’,它调用generateData()并显示数据,但我希望能够操作该数据并将其传递给另一个小部件BarGraph,后者将生成动态数据的条形图。
下面是我的BarGraph小部件:
`
class BarGraph extends StatefulWidget {
BarGraph({Key? key, required this.tableData}) : super(key: key);
List<Post>? tableData;
@override
State<BarGraph> createState() => _BarGraphState();
}`
这里的问题是,TabelData根本无法访问,但我希望能够获得标题、数字和数字,以便能够在图表中显示它们。我试过使用getter,但是null check操作符不能使用它。我完全被困住了,任何帮助都将不胜感激!
发布于 2022-11-17 03:45:56
找到答案了!通过使用widget.tableData,我能够访问传递给该变量的数据。
发布于 2022-11-17 02:58:25
如何使用任何国家管理方法,如:集团,提供者等。使用这些库可以解决您的问题,并使您的代码更有组织。例如,使用For看起来就像
class ManagePost extends Cubit<List<Post>> {
List<Post> postList = [];
void addPost(Post post) {
postList.add(Post);
emit(postList);
}
void deletePost() {}
void updateVote() {}
void getAllPost() {}
}接下来,将您的视图包装在BlocBuilder中,它将侦听post列表的状态和更新UI。
https://stackoverflow.com/questions/74469343
复制相似问题