【问题标题】:how to display json data in the Table flutter如何在 Table Flutter 中显示 json 数据
【发布时间】:2019-11-15 16:09:16
【问题描述】:

我想将数据库中的所有 json 数据显示到表中,但我很困惑 json 的解决方案是纯文本。我不太了解使用 Map ,因为这会使我的代码出错

            FutureBuilder(
                future: UserController.getActivity(_selectedUser),
                builder: (context, snapshot) {
                  if (snapshot.hasData) {
                    var item = snapshot.data;
                    Map<String, dynamic> activity = item[snapshot]['activity'];
                    //print(snapshot.data.toString());
                    print(activity);
                    return Column(
                      mainAxisSize: MainAxisSize.min,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Table(
                          defaultColumnWidth: FixedColumnWidth(150.0),
                          border: TableBorder(
                            horizontalInside: BorderSide(
                              color: Colors.black,
                              style: BorderStyle.solid,
                              width: 1.0,
                            ),
                            verticalInside: BorderSide(
                              color: Colors.black,
                              style: BorderStyle.solid,
                              width: 1.0,
                            ),
                          ),
                          children: [
                            TableRow(children: [
                              TableCell(
                                child: Row(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceAround,
                                  children: <Widget>[
                                     new Text(snapshot.data["project"]),
                                  ],
                                ),
                              )
                            ])
                          ],
                        )
                      ],
                    );
                  } else {
                    return Center(
                      child: Text("No data displayed"),
                    );
                  }
                },
              ),

当我尝试使用此代码时出现错误 _FutureBuilderState # 71363): 应为“int”类型的值,但具有“AsyncSnapshot”类型之一

【问题讨论】:

    标签: flutter dart flutter-web


    【解决方案1】:

    错误在这一行:

    Map<String, dynamic> activity = item[snapshot]['activity'];
    

    您正在尝试使用 AsyncSnapshot 作为对象的键。您必须仅使用数字作为位置或文字键,具体取决于您作为响应的 Json 的结构。

    【讨论】:

    • 是的,您作为响应的所有数据都在 item 中。
    猜你喜欢
    • 2021-11-13
    • 2021-11-27
    • 1970-01-01
    • 2018-11-21
    • 1970-01-01
    • 2020-02-11
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多