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
201 views
in Technique[技术] by (71.8m points)

Searching in Database - Sqlite Flutter

i miss something in my code, i use an existing database (sqlite) in my mobile app, and i want to add a search bar to it, everythings works fine, but the result in the emulator is like this :

{content: name1}

{content: name2}

{content: name3}

i want just the names, thank you for your help !

This is my code :

String text;
List course;

void _query (text) async {
Database db = await DatabaseHelper.instance.database;
List<Map> result = await db.rawQuery("SELECT content FROM table WHERE content LIKE '%${text}%'");

setState(() {
  result.forEach((element) {
  print(element);
  course = result;
  });

});
} 


 body:Column(
    children: [
      Padding(
        padding: const EdgeInsets.all(10.0),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Icon(
              Icons.search,
              color: Colors.grey,
            ),
            SizedBox(
              width: 20,
            ),
            Container(
              width: MediaQuery.of(context).size.width - 100.0,
              child: TextField(                   
                decoration: InputDecoration(
                  hintText: search ... ,
                ),
                onChanged: (String text) async {
                  _query(text);
                },
              ),
            ),
          ],
        ),
      ),
      Expanded(
        child: ListView.builder(
                padding: const EdgeInsets.all(14.0),
                itemBuilder: (context, index) {
                  return Column(
                    children: [
                      Divider(
                        height: 20,
                      ),
                      Material(
                        color:  Colors.grey,
                        child: ListTile(
                          title: Text(
                            course == null
                                ? 'loading'
                                : '${course[index]}',
                            style: TextStyle(                                    
                                color: Colors.black),
                          ),
                        ),
                      )
                    ],
                  );
                }
              ),
          ),
        ],
      ),
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You can try this

 result.forEach((element) {
    print(element);
    print(element['content']);
    course = result;
  });

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

...