使用 listView.builder 时出现 noSuchMethodError elementAt

noSuchMethodError elementAt while using listView.builder

当我 print(snapshot.data) 数据在那里时,..但是当显示时它出现 like this 这是我的代码

  _showActivities() {
return FutureBuilder(
  future: UserController.getActivityByDate(
      {"date": widget.index.toIso8601String(), "id": widget.user}),
  builder: (context, snapshot) {
    if (snapshot.hasData) {
      if (snapshot.data != null){
        print(snapshot.data);
      return ListView.builder(
          itemCount: snapshot.data.length,
          itemBuilder: (context, position) {
            var item = snapshot.data[position];
            return ListTile(
              title: Text("${item["activity"]["project"]}"),
              subtitle: Text(item["created_at"]),
            );
          });
      }

    } 
      return Text("no data displayed");

  },
);

这是我要显示的数据

[


{
    "id": 114,
    "id_user": 114,
    "activity": {
      "code": 2003,
      "project": "Sangat Damai Sejahtera",
      "activity": {
        "name": "Laporan Sales Order Trading",
        "id_process_snapshot": 5016
      },
      "code_name": "SaveProcessSnapshot"
    },
    "created_at": "2019-07-12T17:00:13.931592+07:00"
  },

]

先修正以下问题,如果还有更多错误,请更新您的问题,我会尽力改进答案。

snapshot.hasData 是一个布尔值,它永远不会为空。

改变

if (snapshot.hasData != null)

if (snapshot.hasData)

如果你真的想检查 null 使用以下(但 hasData 应该足够了)。

if (snapshot.data != null)

然后修复图像更改中显示的错误

snapshot.data.elementAt(position);

snapshot.data[position]; //if the object being returned is a list

getActivityByDate returns a json 所以最好从 json 创建对象。这是一些伪代码,用于从 JSON.

创建 User 和 Activity 对象
import 'package:json_annotation/json_annotation.dart'; //add json_annotation: ^2.2.0 in your pubspec

@JsonSerializable(nullable: false)
class User extends Object {
  int id, id_user;
  String created_at;
  Activity activity;

  User({this.id,this.id_user,this.activity,this.created_at});

  User _userFromJson(Map<String, dynamic>json){
    return new User(
      id: json["id"] as int,
      id_user: json["id_user"] as int,
      activity: new Activity()._activityFromJson(json["activity"]),
      created_at: json["created_at"] as String,
    );
  }
}
class Activity extends Object {
  int code, id_process_snapshot;
  String project, name, code_name;

  Activity({this.code,this.name, this.id_process_snapshot,this.project,this.code_name});

  Activity _activityFromJson(Map<String, dynamic>json){
    return new Activity(
      code: json["code"] as int,
      project: json["project"] as String,
      name: json["activity"]["name"] as String,
      id_process_snapshot: json["activity"]["id_process_snapshot"] as int,
      code_name:  json["activity"]["code_name"] as String,
    );
  }
}

返回您的代码,通过 _userFromJson() 方法

传递您的 json
User user = User()._userFromJson(snapshot.data);

现在您将能够使用

创建您的 listTiles
return ListTile(
              title: Text("${user.project} -${user.activity.code_name;}"),
              subtitle: Text("${user.created_at}"),
            );

记住 json 区分大小写,因此请仔细检查您是否正确拼写了键。

我认为您正在尝试从数据库中获取值,或者 api 尝试下面的代码函数 _getAllValues() return 将所有列表传递给构建器

body: FutureBuilder<List<String>>(
    future: _getAllValues(),
      builder:(BuildContext context, AsyncSnapshot<List<String>> list){
      if(list.hasData){
        return new ListView.builder(
            itemCount: list.data.length,
            itemBuilder:(context, index){
              return ListTile(
                title: Text(list.data[index]),
              );

            });
      }