在数据库中搜索-Sqlite Flutter [英] Searching in Database - Sqlite Flutter

查看:90
本文介绍了在数据库中搜索-Sqlite Flutter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我错过了代码中的某些内容,我在移动应用程序中使用了现有的数据库(sqlite),并且我想向其中添加搜索栏,一切正常,但是在模拟器中的结果是这样的:

{content:name1}

{content:name2}

{content:name3}

我只想要名字, 谢谢您的帮助!

这是我的代码:

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),
                          ),
                        ),
                      )
                    ],
                  );
                }
              ),
          ),
        ],
      ),

解决方案

您可以尝试

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

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),
                          ),
                        ),
                      )
                    ],
                  );
                }
              ),
          ),
        ],
      ),

解决方案

You can try this

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

这篇关于在数据库中搜索-Sqlite Flutter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆