如何使用FutureBuilder过滤ListView?

问题描述

我是Flutter编程的初学者。 我调用一个Web API来获取ListView项目。我需要对此ListView进行过滤。 当我在搜索框中输入任何文本时,将显示“正在等待结果”世界,然后再次呈现列表,而没有任何过滤结果。我在搜索框中输入的字母都会调用Web API 我厌倦了许多解决方案,但没有一个我有用。 代码是:

import 'package:Flutter/cupertino.dart';
import 'package:Flutter/material.dart';
import 'package:Flutter/widgets.dart';
import 'package:soft_ta/Models/EmployeeExceptionModel.dart';
import 'package:soft_ta/Widget/sharedWidget.dart' as shared;
import 'package:easy_localization/easy_localization.dart';
import 'package:Flutter_slidable/Flutter_slidable.dart';
import 'package:soft_ta/HelperClass.dart';
import 'package:soft_ta/Remote/WepAPI.dart';

class EmployeesExceptios extends StatefulWidget {
 @override
 State<StatefulWidget> createState() {
return new _EmployeesExceptiosstate();
  }
}

class _EmployeesExceptiosstate extends State<EmployeesExceptios> {
  TextEditingController _textController = TextEditingController();

  List<EmployeeExceptionModel> employeeExceptionModelList =
      new List<EmployeeExceptionModel>();

  final _scaffoldKey = GlobalKey<ScaffoldState>();
  HelperClass helper = new HelperClass();

  List<EmployeeExceptionModel> newDataList = new List<EmployeeExceptionModel>();

  onItemChanged(String value) {
    setState(() {
      newDataList = employeeExceptionModelList
          .where((a) =>
              a.EmployeeId.toString() == (value.toString().toLowerCase()))
          .toList();
    });
  }

  @override
  Widget build(BuildContext context) {
    final height = MediaQuery.of(context).size.height;

    return FutureBuilder(
        future: Future.wait([_getEmployeeException()]),builder: (BuildContext context,AsyncSnapshot<List<dynamic>> snapshot) {
          switch (snapshot.connectionState) {
            case ConnectionState.none:
              return new Text('Press button to start');
            case ConnectionState.waiting:
              return new Text('Awaiting result...');
            default:
              if (snapshot.hasError)
                return new Text('Error: ${snapshot.error}');
              else {
                employeeExceptionModelList = snapshot.data[0];
                newDataList = employeeExceptionModelList;

                return Scaffold(
                    key: _scaffoldKey,body: Container(
                      decoration: Boxdecoration(
                          gradient: LinearGradient(
                              begin: Alignment.topRight,end: Alignment.bottomLeft,colors: [Colors.white,Colors.blueGrey])),height: height,child: Stack(
                        children: [
                          Container(
                            padding: EdgeInsets.only(top: 20),child: Column(
                              children: [
                                shared.divider(
                                    value:
                                        "EmployeeExceptions".tr().toString()),],),Padding(
                            padding: EdgeInsets.only(top: height * .10),child: TextField(
                              controller: _textController,decoration: Inputdecoration(
                                hintText: 'Search Here by EmployeeId...',onChanged: onItemChanged,Padding(
                            padding: EdgeInsets.only(top: height * .20),child: Container(
                              // height: height,padding: EdgeInsets.symmetric(horizontal: 20),child: Container(
                                child: ListView.separated(
                                    separatorBuilder: (context,index) =>
                                        Divider(
                                          color: Colors.black,itemCount: newDataList.length,itemBuilder: (context,int index) {
                                      return Slidable(
                                        actions: <Widget>[
                                          IconSlideAction(
                                              icon: Icons.more,caption: 'MORE',color: Colors.blue,onTap: () {
                                                print(
                                                "More ${newDataList[index]} is Clicked");
                                              }),secondaryActions: <Widget>[
                                          IconSlideAction(
                                              icon: Icons.clear,color: Colors.red,caption: 'Cancel',onTap: () {
                                                print(
                                                    "Cancel ${newDataList[index]} is Clicked");
                                              })
                                        ],child: ListTile(
                                          // leading: Icon(Icons.message),title: Text(
                                            "${newDataList[index].EmployeeId}",style: TextStyle(
                                                fontSize: 16.0,fontWeight: FontWeight.bold,fontFamily: 'Arial'),subtitle: Column(
                                            crossAxisAlignment:
                                                CrossAxisAlignment.start,children: [
                                              Text(
                                                "${newDataList[index].FromDT}",style: TextStyle(
                                                    color: Colors.blueGrey,fontSize: 13.0),Text(
                                                "${newDataList[index].ExceptionName}",trailing: Icon(Icons.arrow_back),actionPane: SlidableDrawerActionPane(),);
                                    }),));
              }
          }
        });
  }

  Future<List<EmployeeExceptionModel>> _getEmployeeException() async {
    var _wepAPI = new WebAPI();
    List<EmployeeExceptionModel> lst = new List<EmployeeExceptionModel>();
    EmployeeExceptionModel objEmployeeExceptionModel;
    List value =
        await _wepAPI.getEmployeesExceptions(helper.getCurrentUserId());
    value.forEach((element) {
      objEmployeeExceptionModel = new EmployeeExceptionModel();
      objEmployeeExceptionModel.AssignmentId = element["AssignmentId"];
      objEmployeeExceptionModel.EmployeeId = element["EmployeeId"];
      objEmployeeExceptionModel.EmployeeName = element["EmployeeName"];
      objEmployeeExceptionModel.AprvStsId = element["AprvStsId"];
      objEmployeeExceptionModel.ExceptionName = element["ExceptionName"];
      objEmployeeExceptionModel.ExceptionTypeName =
          element["ExceptionTypeName"];
      objEmployeeExceptionModel.FromDT = element["FromDT"];
      objEmployeeExceptionModel.ToDT = element["ToDT"];
      lst.add(objEmployeeExceptionModel);
    });

    return lst;
  }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)