Jeevan Shrestha
Jeevan Shrestha

Reputation: 273

flutter not giving snapshot length and accessing snapshot data

I am fetching data from rest API and tried to build.I tried to find snapshot length but got an error

The getter 'length' isn't defined for the type 'Object'. Try importing the library that defines 'length', correcting the name to the name of an existing getter, or defining a getter or field named 'length'

I set itemCount value to default 1 and call my API and tried to build but got an error The operator '[]' isn't defined for the type 'Object'. Try defining the operator '[]'

the FutureBuilder code:

FutureBuilder(future:fetchPost(),builder: ((context, snapshot) {
            return ListView.builder(itemCount:snapshot.data!.length,itemBuilder: (context,index){
              return  ListTile(
                title: Text(snapshot.data![index].status),
              );
            });
          }),
          )

The data from my API:

[
    {
        "_id": "6304e73ecdc5d350cc33e902",
        "userId": "6304e42231ef2e7a4dec924d",
        "posts": [
            {
                "postType": "image",
                "post": "https://www.theskinnybeep.com/wp-content/uploads/2019/01/Versace-Man-2019.jpg",
                "_id": "6304e73ecdc5d350cc33e903"
            },
            {
                "postType": "image",
                "post": "https://www.theskinnybeep.com/wp-content/uploads/2019/01/Versace-Man-2019.jpg",
                "_id": "6304e73ecdc5d350cc33e904"
            }
        ],
        "status": "testing status",
        "comments": [
            {
                "commentText": "Testing comment",
                "commentAt": "2022-08-23T14:41:55.646Z",
                "commentReplys": [
                    {
                        "userId": "6304e02d481e08d44e618d41",
                        "replyText": "Testing comment",
                        "replyAt": "2022-08-23T14:41:55.646Z",
                        "replyLikes": [
                            {
                                "userId": "6304e02d481e08d44e618d41",
                                "_id": "6304e73ecdc5d350cc33e907"
                            }
                        ],
                        "_id": "6304e73ecdc5d350cc33e906"
                    },
                    {
                        "userId": "6304e02d481e08d44e618d41",
                        "replyText": "reply text testing",
                        "replyAt": "2022-08-23T15:57:51.259Z",
                        "_id": "6304f90191c32e0deac663b8",
                        "replyLikes": []
                    }
                ],
                "commentLikes": [],
                "commentId": "bc174de0-22f1-11ed-9c5d-23d89a83ff32",
                "_id": "6304e73ecdc5d350cc33e905"
            },
            {
                "commentText": "Testing comment",
                "commentAt": "2022-08-23T15:02:11.123Z",
                "commentId": "90928740-22f4-11ed-b912-e99836187b6d",
                "_id": "6304ec67825b5926f0f074cf",
                "commentReplys": [
                    {
                        "userId": "6304e02d481e08d44e618d41",
                        "replyText": "reply text testing",
                        "replyAt": "2022-08-23T15:57:51.259Z",
                        "_id": "6304f90191c32e0deac663b8",
                        "replyLikes": []
                    }
                ],
                "commentLikes": []
            },
            {
                "commentText": "Testing comment",
                "commentAt": "2022-08-23T15:02:11.123Z",
                "commentId": "90928740-22f4-11ed-b912-e99836187b6d",
                "_id": "6304ec81825b5926f0f074d1",
                "commentReplys": [
                    {
                        "userId": "6304e02d481e08d44e618d41",
                        "replyText": "reply text testing",
                        "replyAt": "2022-08-23T15:57:51.259Z",
                        "_id": "6304f90191c32e0deac663b8",
                        "replyLikes": []
                    }
                ],
                "commentLikes": []
            }
        ],
        "likes": [
            {
                "userId": "6304e42231ef2e7a4dec924d",
                "_id": "63052dc7a1728d463769681b"
            }
        ],
        "__v": 0
    }
]

fetchPost method:

 Future<Posts> fetchPost()async{
    final response=await http.get(Uri.parse("http://10.0.2.2:8000/posts"));
    if(response.statusCode == 200){
    final posts=jsonDecode(response.body.toString());
    return Posts.fromJson(posts);
    }
    else{
      print("error!");
      throw new Exception("error!");
    }
   }

How to solve this.Thanks in advance.

Upvotes: 0

Views: 51

Answers (1)

Dung Ngo
Dung Ngo

Reputation: 1472

You need to define the type parameter for the FutureBuilder

FutureBuilder<Posts>( // type Posts in your case
  future:fetchPost(),
  builder: ((context, snapshot) {

  })
)

Upvotes: 1

Related Questions