使用textfield flutter搜索数据库中的数据

时间:2019-08-19 10:58:52

标签: flutter dart


我有一个数据库,我想制作一个搜索系统,并在用户键入元素标题后将数据显示在屏幕上。
检查下面的代码,这就是我尝试实现的方法
我尝试这样做:

  Widget searchBar(title) {
    return FutureBuilder<List<Task>>(
      future: DBHelper().getTasks(),
      builder: (BuildContext context, AsyncSnapshot<List<Task>> snapshot) {
        print('entered'); //does not work
        if (snapshot.hasData) {
          var data = snapshot.data;
          print('entered'); //does not work
          return Container(
            child: Column(
              children: data.map((task) {
                print(task.title);
                if (task.title == title) {
                  print('found'); // if user typed correct title than it should print 'found'
                } else {
                  print('not found');
                }
              }).toList(),
            ),
          );
        }
      },
    );
  }
  Widget appBar(device) {
    searchAppBar(device) {
      return Expanded(
        child: TextField(
          onChanged: (data) {
            searchBar(data); //pass data to searchBar, data that user passed to an input.
          },

      );
    }

    return Container( 
      child: Container(
        child: Row(
          children: <Widget>[
           searchAppBar(device), // here I display my input
          ],
        ),
      ),
    );
  }

添加了DBHelper()。getTasks()方法

  Future<List<Task>> getTasks() async {
    var dbClient = await db;
    List<Map> maps = await dbClient.query(TABLE, columns: [
      ID,
      NAME,
      TITLE,
      SUBTASKS,
    ]);
    List<Task> tasks = [];
    if (maps.length > 0) {
      for (int i = 0; i < maps.length; i++) {
        tasks.add(Task.fromMap(maps[i]));
      }
    }
    return tasks;
  }

searchBar中的代码是几天前编写的,并且可以正常工作,在此示例中不起作用):

0 个答案:

没有答案