【问题标题】:How to use a local variable value out it function in flutter?如何在颤振中使用局部变量值?
【发布时间】:2020-05-17 13:10:03
【问题描述】:

我收到了两个值作为函数的参数,现在我必须从该函数中使用该值,但我收到了这些值的空值。

String title;
String type;


void _addNewMedicine(String medTitle, String medType) {

title = medTitle;   // the value of medTitle is assigned to title

type = medType;     // the value of medType is assigned to type

final newMed = Items(productname: medTitle, type: medType);
setState(() {
    _userMedicine.add(newMed);     
    },
);
}  

如果我使用print(medTitle)print(medType),则输出为null

所以请人帮忙!!!

提前谢谢你...

【问题讨论】:

  • 你确定你在调用这个方法之后访问那些变量吗?

标签: android-studio flutter dart


【解决方案1】:

将您的赋值语句移动到 setState 函数中,以便重建状态。在不重建状态的情况下,其他小部件和函数将使用旧值。

setState(
      () {
        title = medTitle;   // the value of medTitle is assigned to title
        type = medType; 
        _userMedicine.add(newMed);
      },
    );

【讨论】:

    猜你喜欢
    • 2019-07-28
    • 2021-03-29
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 2020-10-17
    • 2021-12-04
    • 2021-08-03
    • 1970-01-01
    相关资源
    最近更新 更多