有条件的小部件在扑朔迷离后无效或变化
我只是在编写一个简单的应用程序。我遇到了需要有条件地显示小部件的情况。我正在使用Google表的应用程序脚本API获取数据。我想在加载过程中显示“加载...”文本,并希望在完成请求后将其隐藏。
以下代码工作,如果我在/使用Android Studio 内运行它。但是,如果我构建APK并将其安装在设备上,则仅在屏幕上显示“加载...”文本,尽管数据已加载并存储在listresponse中。
class HomePage extends StatefulWidget {
const HomePage({Key? key, required this.title, required this.baseUrl}) : super(key: key);
final String title;
final String baseUrl;
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
late List listResponse = [];
Future categories() async {
var url = widget.baseUrl + 'categories';
http.Response response = await http.get(Uri.parse(url));
if(response.statusCode == 200){
setState(() {
listResponse = json.decode(response.body);
print(listResponse.toString());
});
}
}
@override
void initState(){
categories();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: TopBar(title: widget.title),
body: listResponse.isEmpty ? Center(child: Text('Loading...')) : Center(child: Text(listResponse.toString()))
);
}
}
我搜索了该解决方案,并在Stackoverflow上找到了一些示例。随着应用程序在开发中的预期运行时,可能是问题。感谢您的帮助。
I am just writing a simple application using flutter. I came across a situation where I need to display widgets conditionally. I am fetching data using app script API from Google Sheets. I want to show the "Loading..." text during loading and want to hide it after completing the request.
The following code works as expected if I run it within/using Android Studio. But If I build the APK and install it on my device, only "Loading..." text is showing on the screen, although the data is loaded and stored in listResponse.
class HomePage extends StatefulWidget {
const HomePage({Key? key, required this.title, required this.baseUrl}) : super(key: key);
final String title;
final String baseUrl;
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
late List listResponse = [];
Future categories() async {
var url = widget.baseUrl + 'categories';
http.Response response = await http.get(Uri.parse(url));
if(response.statusCode == 200){
setState(() {
listResponse = json.decode(response.body);
print(listResponse.toString());
});
}
}
@override
void initState(){
categories();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: TopBar(title: widget.title),
body: listResponse.isEmpty ? Center(child: Text('Loading...')) : Center(child: Text(listResponse.toString()))
);
}
}
I have searched for the solution and found some examples on stackoverflow. What could be the issue as the app is running as expected in development. I appreciate the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用条件值显示加载并结果,以
bool issuccess = false
以及在statuscode == 200 SetState
中,使其issuccess = true
获取数据时,它将正常工作。
或者,您也可以使用
futureBiulder
You can use condition value to show loading and result, Take
bool isSuccess = false
and in yourstatusCode==200 setState
make itisSuccess = true
It will work fine when data is fetched.
Or you can also use
FutureBiulder