动态添加的小部件状态无效

发布于 2025-02-04 20:59:56 字数 927 浏览 1 评论 0原文

我正在开发一项功能,用户可以按下按钮将新的文本字段添加到屏幕上。每组文本字段都存储在其自己的状态小部件中。添加新小部件的删节代码如下:

List<EducationField> fieldList = [];
List<GlobalKey<EducationFieldState>> keyList = [];

// Function that adds new widgets to the list view
onTap: () {
    GlobalKey<EducationFieldState> key = new GlobalKey<EducationFieldState>();

    setState(() {
        fieldList.add(EducationField(key: key));
        keyList.add(key);
    });
},

我可以动态地添加新的小部件。但是,当我尝试访问小部件的状态时,我会发现一个错误,说相应的小部件的状态为无效。每个小部件状态中都有一个函数,可以从其文本字段中获取值。我用来做的代码也如下所示:

void _getUserData(){
    List<EducationModel> modelList = [];

    for(int i = 0; i < fieldList.length; i++){
      modelList.add(keyList[i].currentState!.getData()); // this line is causing the error
      modelList.last.printModel();
    }
}

我对此问题进行了大量研究,但仍然不知道为什么我会遇到空错误。我的方法是错误的还是更小的?如有必要,我还可以提供更多代码。

提前致谢!

I am developing a feature where users can press a button to add a new group of text fields to the screen. Each group of text fields is stored in its own stateful widget. The abridged code to add the new widget is shown below:

List<EducationField> fieldList = [];
List<GlobalKey<EducationFieldState>> keyList = [];

// Function that adds new widgets to the list view
onTap: () {
    GlobalKey<EducationFieldState> key = new GlobalKey<EducationFieldState>();

    setState(() {
        fieldList.add(EducationField(key: key));
        keyList.add(key);
    });
},

I can dynamically add the new widgets just fine. However when I try to access the state of the widgets, I get an error saying that the state of the respective widget is null. There is a function in each widget state that gets the values from their text fields. The code I'm using to do that is also shown below:

void _getUserData(){
    List<EducationModel> modelList = [];

    for(int i = 0; i < fieldList.length; i++){
      modelList.add(keyList[i].currentState!.getData()); // this line is causing the error
      modelList.last.printModel();
    }
}

I have done a lot of research on this issue and still have no idea why I am getting a null error. Is my approach wrong or is it something more minor? I can also give more code if necessary.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉城メ夜风 2025-02-11 20:59:56

结帐 GlobalKey docs

您刚刚创建了动态添加的文本小部件尚不存在,并且您正在尝试将其添加到小部件树中。因此,它没有当前状态。

也许这有帮助?

keyList[i].currentState?.getData() ?? '' // I'm guessing getData return a String

您可以尝试的其他东西:

// only call _getUserData() after widget finished building
WidgetsBinding.instance!.addPostFramecallback(() => _getUserData());

Checkout GlobalKey docs.

Your dynamically added Text widget doesn't exist in the Widget tree yet, as you just created it and you're trying to add it to the Widget tree. So it doesn't have a current state.

Maybe this helps?

keyList[i].currentState?.getData() ?? '' // I'm guessing getData return a String

Something else you could try:

// only call _getUserData() after widget finished building
WidgetsBinding.instance!.addPostFramecallback(() => _getUserData());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文