我不能用.obs改变任何变量的状态,RxType和Rx ();
当我试图改变它时,它会给出错误,不能将值类型int分配给变量类型RxInt。下面是代码:
class StateController extends GetxController {
late String sunrise;
late String sunset;
late int temperatureDegree;
late int maxDegree;
late int minDegree;
late double windSpeed;
late int humidity;
void updateUI(dynamic weatherDataInput) {
sunrise = weatherDataInput["current"]["sunrise"];
sunset = weatherDataInput["current"]["sunset"];
temperatureDegree = weatherDataInput["current"]["temp"];
maxDegree = weatherDataInput["daily"][0]["temp"]["max"];
minDegree = weatherDataInput["daily"][0]["temp"]["min"];
windSpeed = weatherDataInput["current"]["wind_speed"];
humidity = weatherDataInput["current"]["humidity"];
}
}
试试这个方法,
class NameController extends GetxController{
final sunrise = ''.obs;
void updateSomeText(){
sunrise('Text updated'); //or sunrise(weatherDataInput["current"]
//["sunrise"].toString());
}
}
然后更新它,尝试用Obx包装它,例如:
final controller = Get.put(NameController());
Obx(
()=> Text(controller.sunrise.value)
),
您可以像这样在updateUI()的末尾使用update()
方法:
void updateUI(dynamic weatherDataInput) {
sunrise = weatherDataInput["current"]["sunrise"];
sunset = weatherDataInput["current"]["sunset"];
temperatureDegree = weatherDataInput["current"]["temp"];
maxDegree = weatherDataInput["daily"][0]["temp"]["max"];
minDegree = weatherDataInput["daily"][0]["temp"]["min"];
windSpeed = weatherDataInput["current"]["wind_speed"];
humidity = weatherDataInput["current"]["humidity"];
update();
}
,然后在UI中使用GetBuilder,或者,你应该将变量声明为Rx,例如:
RxString sunrise = "".obs;
RxString sunset = "".obs;
并在UI中使用观察者小部件:
Obx(
()=> Text(controller.sunset.value)
)
当可观察对象(日出和日落)发生变化时,这将自动更新你的UI。.