Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
275 views
in Technique[技术] by (71.8m points)

android - snapshot.hasData return an empty array

I'm trying to get the data to display in List view but unfortunately i'm facing the error as the snapshot return empty values

Below is the code for my API

 static Future<List<Permission>> getPermissionData() async {
    String ruhusa = 'permission';
    var response = await http.get(mainUrl + ruhusa,
          headers: setHeader(accessKey.accessKey));

      List<Permission> permissions = [];
      if(response.statusCode == 200){

        print(response.statusCode);

        Map<String, dynamic> body = jsonDecode(response.body);
        // print(response.body);
        for(var item in body['msg']){
          Permission permission = Permission.fromJson(item);
          permissions.add(permission);
          // return permissions; 
        }
        return permissions;
      }  
      // print(permissions); 
      return permissions; 

  }

This is the code where i want to display using future builder

 body: FutureBuilder(
        future: PermissionService.getPermissionData(),
        builder: (BuildContext context, AsyncSnapshot<List<Permission>> snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            if (snapshot != null) {
              print("This is ${snapshot.data}");
              return ListView.builder(
                itemCount: snapshot.data?.length ?? 0,
                itemBuilder: (context, index) {
                  return GestureDetector(
                    onTap: () {
                      Navigator.pushNamed(context, permissionSpecificRoute,
                          arguments: snapshot.data[index]);
                    },
                    child: PermissionCard(
                      permission: snapshot.data[index],
                    ),
                  );
                },
              );
            } else if(snapshot.hasError) {
               return Center(child: Text("Oops error issues is ${snapshot.hasError}"));
            }
            else {
              return Center(child: Text('No permissions requested'));
            }
          } else {
            return PageLoader();
          }
        },
      ),

When I try to print it in my console it return

This is []

Anyone who can help please what should i do to make the snapshot receive the real data


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...