有条件的小部件在扑朔迷离后无效或变化

发布于 2025-01-21 13:12:44 字数 1364 浏览 4 评论 0原文

我只是在编写一个简单的应用程序。我遇到了需要有条件地显示小部件的情况。我正在使用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 技术交流群。

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

发布评论

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

评论(1

许久 2025-01-28 13:12:44

您可以使用条件值显示加载并结果,以bool issuccess = false以及在statuscode == 200 SetState中,使其issuccess = true

class _HomePageState extends State<HomePage> {
  late List listResponse = [];
  bool isSuccess = false;

  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);
        isSuccess = true;
        print(listResponse.toString());

      });
    }
  }

  @override
  void initState(){
    categories();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: TopBar(title: widget.title),
      body: isSuccess  ? Center(child: Text('Loading...')) :  Center(child: Text(listResponse.toString()))
    );
  }

获取数据时,它将正常工作。
或者,您也可以使用futureBiulder

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
        future: categories(),
        builder: (context, snapshot) {
          if (snapshot.hasError) {
            return Text(
              'There was an error :(',
              style: Theme.of(context).textTheme.headline,
            );
          } else if (snapshot.hasData) {
            var count = json.decode(snapshot.data).length;
            return Center(child: Text(listResponse.toString()));
          } else {
            return Center(child: Text('Loading...')) 
          }
        });
  }

You can use condition value to show loading and result, Take bool isSuccess = false and in your statusCode==200 setState make it isSuccess = true

class _HomePageState extends State<HomePage> {
  late List listResponse = [];
  bool isSuccess = false;

  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);
        isSuccess = true;
        print(listResponse.toString());

      });
    }
  }

  @override
  void initState(){
    categories();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: TopBar(title: widget.title),
      body: isSuccess  ? Center(child: Text('Loading...')) :  Center(child: Text(listResponse.toString()))
    );
  }

It will work fine when data is fetched.
Or you can also use FutureBiulder

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
        future: categories(),
        builder: (context, snapshot) {
          if (snapshot.hasError) {
            return Text(
              'There was an error :(',
              style: Theme.of(context).textTheme.headline,
            );
          } else if (snapshot.hasData) {
            var count = json.decode(snapshot.data).length;
            return Center(child: Text(listResponse.toString()));
          } else {
            return Center(child: Text('Loading...')) 
          }
        });
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文