使用listView.builder时出现noSuchMethodError elementAt

时间:2019-07-17 07:03:07

标签: flutter flutter-web

当我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"
  },

]

2 个答案:

答案 0 :(得分:0)

首先解决以下问题,然后如果存在更多错误,请更新您的问题,我将尝试改善答案。

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返回一个json,因此最好从json创建对象。这是一些伪代码,用于从JSON创建用户和活动对象。

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);

现在,您将可以使用

来创建列表标题
return ListTile(
              title: Text("${user.project} -${user.activity.code_name;}"),
              subtitle: Text("${user.created_at}"),
            );

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

答案 1 :(得分:0)

我认为您正在尝试从数据库或api获取值,请尝试使用下面的代码,这里的函数_getAllValues()将所有列表返回给构建器

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]),
              );

            });
      }