Hakim Rahim
Hakim Rahim

Reputation: 17

How to fetch data into listview?

how do i fetch my data inside listview..... i been trying fetch list of data but it seems doesnt work for me or maybe the way i fetch it for data is wrong and for now i just fetch a raw data inside the listview that it fetch all row data inside one list.... can somebody help me how to fetch data inside the list

this is my code

FutureBuilder(
              future: loadingCsvData(path),
              builder: (context,
                  AsyncSnapshot<Map<int, Map<String, dynamic>>> snapshot) {
                print(snapshot.data.toString());
                return snapshot.hasData
                    ? ListView(
                        scrollDirection: Axis.vertical,
                        shrinkWrap: true,
                        children: [
                          Card(
                            child: Column(children: <Widget>[
                              ListTile(
                                title: Text(snapshot.data.toString()),
                              )
                            ]),
                          )
                        ],
                      )
                    : Center(
                        child: CircularProgressIndicator(),
                      );
              },
            ),

my list model

static Map<String, dynamic> userToMap(List<dynamic> row) {
    return {
      if (row[0] != null) 'No': row[0] as int,
      if (row[1] != null) 'Name': row[1] as String,
      if (row[2] != null) 'Code': row[2] as String
    };
  }

  // A map with an int key and Map<String, dynamic> value
  static Map<int, Map<String, dynamic>> userListToMap(
      List<List<dynamic>> userList) {
    userList.removeAt(
        0); //If you want to remove the first row containing the headers
    return userList.map((user) => userToMap(user)).toList().asMap();
  }

Upvotes: 0

Views: 217

Answers (2)

Hamza Ali
Hamza Ali

Reputation: 1

First of all, I'm not sure about Android, but when you return JSON, it's good manner to set proper headers.

By now your php script (http://pixography.netai.net/json.php) returns following:

HTTP/1.1 200 OK
Date: Sun, 06 Dec 2015 20:37:02 GMT
Server: Apache
X-Powered-By: PHP/5.2.17
Content-Length: 238
Connection: close
Content-Type: text/html

Try putting header('Content-type: application/json; charset=utf-8'); before any output in your script, also check for UTF-8 BOM issue, because it breaks proper header output in php.

At least you will get proper headers for your JSON.

--

Another thing that I see in your php code - you array keys are numeric, I'm not quite sure, but I think for JSON object keys should be strings. So maybe you should try $response[$row[0]] = array("Name"=>$row[0],"Location"=>$row[1]); instead ?

--

I also think that you should put mysqli_close($con); before echo, for better performance. After you fulfilled your data for output, there is no reason at all to keep the MySQL connection open.

Upvotes: 0

Hemal Moradiya
Hemal Moradiya

Reputation: 2097

For example this is your list

  Map _data = {
    0: {'No': 1, 'Name': 'Ali', 'Code': 'A123'},
    1: {'No': 2, 'Name': 'Abu', 'Code': 'B456'},
    2: {'No': 3, 'Name': 'Amir', 'Code': 'C789'},
    3: {'No': 4, 'Name': 'Safe', 'Code': 'D098'},
    4: {'No': 5, 'Name': 'Alif', 'Code': 'E765'}
  };

And you can fetch data from this list to ListView.builder like this

   ListView.builder(
      itemCount: _data.length,
      shrinkWrap: true,
      itemBuilder: (context, index) {
        return Column(
          children: [
            Text(_data[index]['Name']),
            Text(_data[index]['Code']),
          ],
        );
      },
    );

Upvotes: 1

Related Questions