1

我想将数据库中的所有 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): Expected a value of type 'int', but has one type of 'AsyncSnapshot'

4

1 回答 1

0

错误在这一行:

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

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

于 2019-07-05T04:11:41.623 回答