颤振刷新Futurebuilder和Listview.builder

时间:2019-08-05 11:51:44

标签: api listview asynchronous checkbox flutter

所以我有一个包含Listview.builder的Futurebuilder。这是一个清单。每个对象都是一个容器,该容器具有一行,该行具有一个CheckBox和一个从Django rest api获取的字符串。这是api的结构:

class Checker {
  final int id;
  final String name;
  final bool checked;
  final int sender;
  final dynamic sender_obj;
  final List canview;
  final List canview_obj;


  Checker(this.id, this.name, this.checked, this.sender, this.sender_obj, this.canview, this.canview_obj);

  Checker.fromJson(Map<String, dynamic> j)
      : id = j['id'],
        name = j['name'],
        checked = j['checked'],
        sender = j['sender'],
        sender_obj = j['sender_obj'],
        canview = j['canview'],
        canview_obj = j['canview_obj'];
}

这里也是Future>函数,我调用该函数以从api中获取数据(这是未来:Futurebuilder中的____,请注意,这是通过承载令牌进行的身份验证请求,可在登录应用程序时访问。请求中的承载令牌):

  Future<List<Checker>> _getCheckerData() async {
    var requestHeaders = {HttpHeaders.authorizationHeader: "Bearer $bearer"};
    http.Response data = await http.get('http://localhost:8000/api/checkers/?task=$id', headers: requestHeaders);
    List jsonData = json.decode(data.body);
    List<Checker> allCheckers = jsonData.map<Checker>((j) => Checker.fromJson(j)).toList();
    return allCheckers;
  }

最后,Futurebuilder返回带有复选框和名称的容器。请注意,复选框可以为True或False。那是从api'checked'对象中获取的!这是代码:

        Container(
          child: FutureBuilder(
            future: _getCheckerData(),
            builder: (context, snapshot) {
              if(snapshot.data != null) {
                return ColumnBuilder(
                itemCount: snapshot.data.length,
                itemBuilder: (context, index) {
                  print(snapshot.data);
                  return Container(
                    margin: EdgeInsets.only(left: 4),
                    child: Row(
                      children: <Widget>[
                        Checkbox(value: snapshot.data[index].checked, onChanged: null),
                    Container(
                      margin: EdgeInsets.only(left: 0),
                      alignment: Alignment.centerLeft,
                      child: Text(snapshot.data[index].name),
                    )
                      ],
                    ),
                  );
                }
              );}
              else return Container();
            }
          ),
        ),

This is an image of the Checklist

当我按下一个复选框时,我想发生什么。用于使应用向api发出PUT请求,然后重新加载将来的构建器并列出view.builder以获取数据。请注意,我需要这样做,因为其他用户需要看到它!

0 个答案:

没有答案