类“ List <User>”没有实例获取程序“ lenght”。接收方:“ _ GrowableList”的实例(长度:5)尝试调用:长度

时间:2020-06-24 10:43:43

标签: json flutter

你好,我收到此错误

Class 'List<User>' has no instance getter 'lenght'.
Receiver: Instance(length:5) of '_GrowableList'
Tried calling: lenght

我不知道问题出在哪里,我在代码中看不到任何问题,并且json文件列表很好,并且没有问题,可能与FutureBuilder有关。

这是json代码

[
  {
    "index": 0,
    "name": "maen",
    "dep": "softwere",
    "pic": "https://shorturl.at/jqsIZ"
  },
  {
    "index": 1,
    "name": "maen",
    "dep": "softwere",
    "pic": "https://shorturl.at/jqsIZ"
  },
  {
    "index": 2,
    "name": "maen",
    "dep": "softwere",
    "pic": "https://shorturl.at/jqsIZ"
  },
  {
    "index": 3,
    "name": "maen",
    "dep": "softwere",
    "pic": "https://shorturl.at/jqsIZ"
  },
  {
    "index": 4,
    "name": "maen",
    "dep": "softwere",
    "pic": "https://shorturl.at/jqsIZ"
  }
]
Future<List<User>> getData() async{
        var data = await http.get('http://maen.mis-web.com/generated.json');
    
        var jsonData = json.decode(data.body);
    
        List<User> users = [];
    
        for(var u in jsonData){
          User user = User(u["index"], u["name"], u["dep"], u["pic"]);
    
          users.add(user);
        }
    
        print(users.length);
    
        return users;
     }
    
    
    
     void initState(){
        getData();
        super.initState();
     }
    
     Widget futureList(){
        return FutureBuilder(
          future: getData(),
          builder: (BuildContext context, AsyncSnapshot snapshot){
            print(snapshot.data);
            if (snapshot.data == null){
              return ColorLoader2(
                color1: Colors.redAccent,
                color2: Colors.green,
                color3: Colors.amber,
              );
            } else {
              return ListView.builder(
                  itemCount: snapshot.data.lenght,
                  itemBuilder: (BuildContext context, int index){
                    return ListTile(
                      leading: CircleAvatar(
                        backgroundImage: NetworkImage(
                            snapshot.data[index].pic
                        ),
                      ),
                      title: Text(snapshot.data[index].name),
                      subtitle: Text(snapshot.data[index].dep),
                    );
                   },
               );
             }
           },
          );
     }

1 个答案:

答案 0 :(得分:1)

 itemCount: snapshot.data.lenght

应该是

 itemCount: snapshot.data.length