dart 在Flutter中使用where和contains过滤列表可以正常工作,但是过滤掉的项不会返回backspace

s6fujrry  于 2023-01-28  发布在  Flutter
关注(0)|答案(1)|浏览(165)

我正在构建我的第一个Flutter应用程序,我试图使用TextFormField在Flutter中用GetX过滤一个TreeType列表。
因此,列表是从API中提取的,我希望使用TreeTypeController.filterTreeTypes(earchString)方法实时过滤它。
不包含searchString的结果会像应该的那样被过滤掉,但是当我在searchString上使用backspace时,移除的项不会放回列表中。
我知道我不知何故需要做一个filteredTreeTypeList的克隆,而不是过滤,然后用结果覆盖它,但我只是不知道如何做到这一点。我曾尝试创建一个新的列表与Map和其他方法,我发现在互联网上,但我一直得到同样的问题。
有人能教我怎么做吗?谢谢!
下面是我的GetxController和视图/屏幕。

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:bonsai_care_app/services/tree_type_service.dart';
import 'package:bonsai_care_app/models/tree_type.dart';

class TreeTypeController extends GetxController {
  dynamic arguments = Get.arguments;
  var treeTypeList = <TreeType>[].obs;
  var isLoaded = true.obs;

  @override
  void onInit() {
    super.onInit();
    fetchAndSetTreeTypeList(arguments);
  }

  void filterTreeTypes(searchString) {
    var filteredTreeTypeList = [];
    filteredTreeTypeList = treeTypeList
        .where((element) =>
            element.name.toLowerCase().contains(searchString.toLowerCase()))
        .toList();
    print(filteredTreeTypeList.length);
    treeTypeList(filteredTreeTypeList);
  }

  Future<void> fetchAndSetTreeTypeList(arguments) async {
    debugPrint('TreeTypesController fetchTreeTypes()');
    try {
      isLoaded(false);
      var tree_types = await TreeTypeService().fetchTreeTypes(arguments);
      if (tree_types != null) {
        treeTypeList(tree_types);
      }
    } catch (error) {
      debugPrint(error);
      throw (error);
    } finally {
      isLoaded(true);
    }
  }
}
import 'package:get/get.dart';
import 'package:flutter/material.dart';
import 'package:bonsai_care_app/controllers/tree_type_controller.dart';
import 'package:bonsai_care_app/views/select_tree_type_details_screen.dart';

class SelectTreeTypeResultsScreen extends StatelessWidget {
  final TreeTypeController treeTypeController = Get.put(TreeTypeController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('select_tree_type_results__heading'.tr),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Obx(
            () {
              return Column(
                children: [
                  Container(
                    child: TextFormField(
                      onChanged: (query) {
                        treeTypeController.filterTreeTypes(query);
                      },
                      decoration: InputDecoration(
                        labelText:
                            'select_tree_type_results__search_input_label'.tr,
                        border: const OutlineInputBorder(),
                      ),
                    ),
                  ),
                  Container(
                    height: 500,
                    width: 200,
                    child: Visibility(
                      visible: treeTypeController.isLoaded.value,
                      replacement: Center(
                        child: CircularProgressIndicator(),
                      ),
                      child: treeTypeController.treeTypeList.length == 0
                          ? Center(
                              child: Text('no_results'.tr),
                            )
                          : ListView.builder(
                              itemCount: treeTypeController.treeTypeList.length,
                              itemBuilder: (context, index) {
                                return ListTile(
                                  title: Text(treeTypeController
                                      .treeTypeList[index].name),
                                  onTap: () {
                                    Get.to(() => SelectTreeTypeDetailsScreen(),
                                        arguments: treeTypeController
                                            .treeTypeList[index]);
                                  },
                                );
                              },
                            ),
                    ),
                  ),
                ],
              );
            },
          ),
        ],
      ),
    );
  }
}
bvuwiixz

bvuwiixz1#

在控制器中声明其他列表:

List<TreeType> treeList = [];

并在API调用中初始化它:

treeList = tree_types;

最后更新您的搜索功能:

void filterTreeTypes(searchString) {
  treeTypeList.value = treeList
    .where((element) =>
        element.name.toLowerCase().contains(searchString.toLowerCase()))
    .toList();
}

相关问题