【问题标题】:Flutter variables in widgets are not updating after fetching from a API从 API 获取后,小部件中的 Flutter 变量未更新
【发布时间】:2021-05-30 02:41:09
【问题描述】:

我正在请求一个 API 并尝试在 UI 中显示响应值,但在分配新收集的数据变量后,它并没有更新值,而是我没有看到任何变化。这是我正在尝试做的代码

Widget buildTop(double height, double width) {
        final Map arguments = ModalRoute.of(context).settings.arguments as Map;
        String user_id = '';
        var name = '';
        var password = '';
        var phoneno = '';
        var uploadid = '';
        var user_type = '';
        if (arguments != null) {
          user_id = arguments['user_id'];
        }
        print(user_id);
    
        Future fetchData() async {
          AuthService().readStaff("staff", "uploadid", user_id).then((val) {
            print(val);
            setState(() {
              name = val.data[0]['Name'];
              password = val.data[0]['password'];
              phoneno = val.data[0]['phoneno'];
              uploadid = val.data[0]['uploadid'];
              user_type = val.data[0]['user_type'];
            });
            print(name);
          });
        }
    
        fetchData();
        @override
        void initState() {
          fetchData();
          // TODO: implement initState
          super.initState();
        }
    
        print('name $name');
        return Container(
          height: height,
          width: width,
          decoration: BoxDecoration(
            color: color,
            borderRadius: BorderRadius.only(
              bottomLeft: Radius.circular(16.0),
              bottomRight: Radius.circular(16.0),
            ),
          ),
          child: Column(
            children: <Widget>[
              Container(
                height: height * 0.5,
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    Container(
                      width: width * 0.3,
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: <Widget>[
                          Text(
                            'Rank $name',
                            style: TextStyle(color: Colors.white),
                          ),
                          SizedBox(height: 5.0),
                          Text(
                            '12',
                            style: TextStyle(
                              color: Colors.white,
                              fontWeight: FontWeight.bold,
                              fontSize: 17.0,
                            ),
                          ),
                        ],
                      ),
                    ),
                    Expanded(
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: <Widget>[
                          Container(
                            height: 140,
                            width: 140,
                            decoration: BoxDecoration(
                              shape: BoxShape.circle,
                              image: DecorationImage(
                                fit: BoxFit.fill,
                                image: new NetworkImage(
                                    "https://i.pinimg.com/originals/18/fd/64/18fd644e9cdf81ab785d606584a384fb.jpg"),
                              ),
                            ),
                          ),
                          SizedBox(height: 15.0),
                          Text(
                            'ID: $uploadid ',
                            style: TextStyle(color: Colors.white70),
                          )
                        ],
                      ),
                    ),
                    Container(
                      width: width * 0.3,
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: <Widget>[
                          Text(
                            '',
                            style: TextStyle(color: Colors.white),
                          ),
                          SizedBox(height: 5.0),
                          Text(
                            '',
                            style: TextStyle(
                              color: Colors.white,
                              fontWeight: FontWeight.bold,
                              fontSize: 17.0,
                            ),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
                          
              Spacer(),
            ],
          ),
        );
      }

我在 AuthService().readStaff("staff", "uploadid", user_id) 处调用 API。当我获取响应数据变量时,它没有得到更新,它仍然是初始分配的空字符串值。当我收到 API 响应时,如何更新变量并将其显示在页面上??

【问题讨论】:

    标签: api flutter http dart request


    【解决方案1】:

    您应该定义一个StatefulWidget,而不是Widget buildTop() 方法。

    class _Top extends StatefulWidget {
      @override
      __TopState createState() => __TopState();
    }
    
    class __TopState extends State<_Top> {
      String user_id = '';
      var name = '';
      var password = '';
      var phoneno = '';
      var uploadid = '';
      var user_type = '';
    
      Future fetchData() async {
        AuthService().readStaff("staff", "uploadid", user_id).then((val) {
          print(val);
          setState(() {
            name = val.data[0]['Name'];
            password = val.data[0]['password'];
            phoneno = val.data[0]['phoneno'];
            uploadid = val.data[0]['uploadid'];
            user_type = val.data[0]['user_type'];
          });
          print(name);
        });
      }
    
      @override
      void initState() {
        fetchData();
        // TODO: implement initState
        super.initState();
      }
    
      @override
      Widget build(BuildContext context) {
        return Container(
          [...]
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-24
      • 1970-01-01
      • 1970-01-01
      • 2020-04-08
      相关资源
      最近更新 更多