2

Im using MS SQL and I want to read data and display it in a ListViewBuilder, but Im having some difficulties.

I dont know how to find itemCount (biggest problem)

I dont know how to display Data in ListTile

query - "SELECT * FROM TabelTest"

Below I will show a part of code and how I want to be shown in app:

// This is to get and display in terminal data from db
    Future<void> read(String query) async {
        var res = await SqlConn.readData(query);
        debugPrint(res.toString());
      }

Data that I need to put in ListTile data shown in terminal

This is how I want to show data (Red underlines are main problem) enter image description here

2 Answers 2

1

try this:

FutureBuilder<List?>(
        future: read("SELECT * FROM TabelTest"),
        builder: (context, snapshot) {
          switch (snapshot.connectionState) {
            case ConnectionState.waiting:
              return Text('Loading....');
            default:
              if (snapshot.hasError)
                return Text('Error: ${snapshot.error}');
              else
                List data = snapshot.data ?? [];
                return ListView.builder(
                    itemCount: data.length,
                    itemBuilder: (context, index) {
                      return Text((data[index] as Map)['name']);
                    });
          }
        })

also change this:

Future<List?> read(String query) async {
     final result = await SqlConn.readData(query);
     return jsonDecode(result) as List;
}
Sign up to request clarification or add additional context in comments.

13 Comments

query is a String that you should add your self in order to get data from db
how did you print that blue screen shot which contain a list?
and how did you get res? by calling await SqlConn.readData(query);? if yes what did you set as query in it?
as you can see in the package example, Query is something like this "SELECT * FROM IP_List", but not this one.
if you tell me what Query you use when creating database, I could help you
|
1

First at all, I would recommend you to map the data into a model, its considered a good practice and avoid possible type casting related bugs.

Second, you could use a FutureBuilder to make the asynchronous call to the SQL database. I also would modify your read method to return to retrieved data of the database and work together with FutureBuilder.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.