【发布时间】:2018-12-19 08:28:21
【问题描述】:
作为 Flutter 的新手,在 Flutter 应用程序中使用 setState 让我非常困惑。在下面的代码中,布尔值searching 和变量resBody 在setState 内部使用。我的问题是为什么只有searching 和resBody 在setState 中?为什么其他变量不可变?
var resBody;
bool searching = false,api_no_limit = false;
String user = null;
Future _getUser(String text) async{
setState(() {
searching = true;
});
user = text;
_textController.clear();
String url = "https://api.github.com/users/"+text;
var res = await http
.get(Uri.encodeFull(url), headers: {"Accept":
"application/json"});
setState(() {
resBody = json.decode(res.body);
});
}
【问题讨论】:
-
这取决于代码在小部件内部或外部的位置。从您的问题无法得出。
-
有什么好的资源来了解这个主题吗?
-
docs.flutter.io/flutter/widgets/State/setState.html, flutterbyexample.com/stateful-widget-lifecycle。我确定有几个教程和 YouTube 视频,但我手头没有链接。我发现youtube.com/watch?v=RS36gBEp8OI 的介绍很好。
-
简单来说,当你改变变量的值时。这是最基本的。
-
但用户变量不在 setState 内,但它仍在应用程序内工作。那么它在 StatefulWidget 中是如何工作的呢? @goops17