0

Following is relevant code snippet:

            TextField(
                      controller: searchController,
                      onChanged: (value) {
                      context
                      .read<PremiseCubit>()
                      .searchPremises(value, allPremises);}
              void searchPremises(String query, 
                                  List<PremiseModel> allPremises) {
              if (query.isEmpty) {
              emit(CollectionSuccess<PremiseModel>(allPremises));
              } else {
            final filteredPremises = allPremises.where((premise) {
            final premiseId = premise.selfRef.id.toLowerCase();
            final street = premise.address.street.toLowerCase();
            final streetNumber = premise.address.streetNumber.toLowerCase();
            final zipCode = premise.address.zipcode.toLowerCase();
            final city = premise.address.city.toLowerCase();
            final ownerId = premise.ownerOrgRef.id.toLowerCase();
            final status = premise.active ? "active" : "inactive";
            final date = premise.created.toString().toLowerCase();
    
            return premiseId.contains(query.toLowerCase()) ||
                street.contains(query.toLowerCase()) ||
                streetNumber.contains(query.toLowerCase()) ||
                zipCode.contains(query.toLowerCase()) ||
                city.contains(query.toLowerCase()) ||
                ownerId.contains(query.toLowerCase()) ||
                status.contains(query.toLowerCase()) ||
                date.contains(query.toLowerCase());
          }).toList();
    
          emit(CollectionSuccess<PremiseModel>(filteredPremises));
        }
      }

      child: BlocBuilder<PremiseCubit, CollectionState>(
                          builder: (context, state) {
                            if (state is CollectionSuccess<PremiseModel>) {
                              List<PremiseModel> premiseModels = state.models;
    
                              return SingleChildScrollView(
                                scrollDirection: Axis.horizontal,
                                physics: const BouncingScrollPhysics(),
                                child: SingleChildScrollView(
                                  physics: const BouncingScrollPhysics(),
                                  scrollDirection: Axis.vertical,
                                  child: DataTable(
                                    headingRowColor: const WidgetStatePropertyAll(
                                        Color.fromARGB(255, 175, 168, 168)),
                                    columns: [
                                      DataColumn(
                                        label: SortableHeader(
                                          title: 'PremiseID',
                                          isAscending: _sortAscendingPremiseID,
                                          onPressed: () {
                                            setState(() {
                                              _sortAscendingPremiseID =
                                                  !_sortAscendingPremiseID;
                                              context
                                                  .read<PremiseCubit>()
                                                  .sortPremises(
                                                    premiseModels,
                                                    (premise) => premise.selfRef.id,
                                                    _sortAscendingPremiseID,
                                                  );
                                            });
                                          },
                                        ),
                                      ),
                                      DataColumn(
                                        label: SortableHeader(
                                          title: 'Street, No.',
                                          isAscending: _sortAscendingStreet,
                                          onPressed: () {
                                            setState(() {
                                              _sortAscendingStreet =
                                                  !_sortAscendingStreet;
                                              context
                                                  .read<PremiseCubit>()
                                                  .sortPremises(
                                                    premiseModels,
                                                    (premise) => int.parse(premise
                                                        .address.streetNumber),
                                                    _sortAscendingStreet,
                                                  );
                                            });
                                          },
                                        ),
                                      ),
                                      DataColumn(
                                        label: SortableHeader(
                                          title: 'ZipCode',
                                          isAscending: _sortAscendingZipCode,
                                          onPressed: () {
                                            setState(() {
                                              _sortAscendingZipCode =
                                                  !_sortAscendingZipCode;
                                              context
                                                  .read<PremiseCubit>()
                                                  .sortPremises(
                                                    premiseModels,
                                                    (premise) => int.parse(
                                                        premise.address.zipcode),
                                                    _sortAscendingZipCode,
                                                  );
                                            });
                                          },
                                        ),
                                      ),
                                      DataColumn(
                                        label: SortableHeader(
                                          title: 'City',
                                          isAscending: _sortAscendingCity,
                                          onPressed: () {
                                            setState(() {
                                              _sortAscendingCity =
                                                  !_sortAscendingCity;
                                              context
                                                  .read<PremiseCubit>()
                                                  .sortPremises(
                                                    premiseModels,
                                                    (premise) =>
                                                        premise.address.city,
                                                    _sortAscendingCity,
                                                  );
                                            });
                                          },
                                        ),
                                      ),
                                      DataColumn(
                                        label: SortableHeader(
                                          title: 'PremiseOwner',
                                          isAscending: _sortAscendingOwner,
                                          onPressed: () {
                                            setState(() {
                                              _sortAscendingOwner =
                                                  !_sortAscendingOwner;
                                              context
                                                  .read<PremiseCubit>()
                                                  .sortPremises(
                                                    premiseModels,
                                                    (premise) =>
                                                        premise.ownerOrgRef.id,
                                                    _sortAscendingOwner,
                                                  );
                                            });
                                          },
                                        ),
                                      ),
                                      DataColumn(
                                        label: SortableHeader(
                                          title: 'Status',
                                          isAscending: _sortAscendingStatus,
                                          onPressed: () {
                                            setState(() {
                                              _sortAscendingStatus =
                                                  !_sortAscendingStatus;
                                              context
                                                  .read<PremiseCubit>()
                                                  .sortPremises(
                                                    premiseModels,
                                                    (premise) =>
                                                        premise.active.toString(),
                                                    _sortAscendingStatus,
                                                  );
                                            });
                                          },
                                        ),
                                      ),
                                      DataColumn(
                                        label: SortableHeader(
                                          title: 'Date',
                                          isAscending: _sortAscendingDate,
                                          onPressed: () {
                                            setState(() {
                                              _sortAscendingDate =
                                                  !_sortAscendingDate;
                                              context
                                                  .read<PremiseCubit>()
                                                  .sortPremises(
                                                    premiseModels,
                                                    (premise) =>
                                                        premise.created.toString(),
                                                    _sortAscendingDate,
                                                  );
                                            });
                                          },
                                        ),
                                      ),
                                    ],
                                    rows: [
                                      for (var i = 0; i < premiseModels.length; i++)
                                        DataRow(
                                          selected: _selectedRows.contains(i),
                                          onSelectChanged: (isSelected) {
                                            setState(() {
                                              if (isSelected ?? false) {
                                                _selectedRows.add(i);
                                                _selectedPremiseIds.add(
                                                    premiseModels[i].selfRef.id);
                                              } else {
                                                _selectedRows.remove(i);
                                                _selectedPremiseIds.remove(
                                                    premiseModels[i].selfRef.id);
                                              }
                                            });
                                          },
                                          cells: [
                                            DataCell(
                                              GestureDetector(
                                                onTap: () {
                                                  Navigator.push(
                                                    context,
                                                    MaterialPageRoute(
                                                      builder: (context) =>
                                                          XXXXXX(
                                                        premiseId: premiseModels[i]
                                                            .selfRef
                                                            .id,
                                                      ),
                                                    ),
                                                  );
                                                },
                                                child: Container(
                                                  padding:
                                                      const EdgeInsets.symmetric(
                                                          horizontal: 8.0),
                                                  child: Text(
                                                    premiseModels[i].selfRef.id,
                                                    style: defaultTextStyle(
                                                        color: Colors.white,
                                                        fontSize: 16),
                                                  ),
                                                ),
                                              ),
                                            ),
                                            DataCell(
                                              Container(
                                                padding: const EdgeInsets.symmetric(
                                                    horizontal: 8.0),
                                                child: Row(
                                                  mainAxisAlignment:
                                                      MainAxisAlignment
                                                          .spaceBetween,
                                                  children: [
                                                    Text(
                                                      '${premiseModels[i].address.street}, ${premiseModels[i].address.streetNumber}',
                                                      style: defaultTextStyle(
                                                          color: Colors.white,
                                                          fontSize: 16),
                                                    ),
                                                  ],
                                                ),
                                              ),
                                            ),
                                            DataCell(
                                              Container(
                                                padding: const EdgeInsets.symmetric(
                                                    horizontal: 8.0),
                                                child: Text(
                                                  premiseModels[i].address.zipcode,
                                                  style: defaultTextStyle(
                                                      color: Colors.white,
                                                      fontSize: 16),
                                                ),
                                              ),
                                            ),
                                            DataCell(
                                              Container(
                                                padding: const EdgeInsets.symmetric(
                                                    horizontal: 8.0),
                                                child: Text(
                                                  premiseModels[i].address.city,
                                                  style: defaultTextStyle(
                                                      color: Colors.white,
                                                      fontSize: 16),
                                                ),
                                              ),
                                            ),
                                            DataCell(
                                              Container(
                                                padding: const EdgeInsets.symmetric(
                                                    horizontal: 8.0),
                                                child: Text(
                                                  premiseModels[i].ownerOrgRef.id,
                                                  style: defaultTextStyle(
                                                      color: Colors.white,
                                                      fontSize: 16),
                                                ),
                                              ),
                                            ),
                                            DataCell(
                                              Container(
                                                padding: const EdgeInsets.symmetric(
                                                    horizontal: 8.0),
                                                child: premiseModels[i].active ==
                                                        true
                                                    ? const Text(
                                                        'Active',
                                                        style: TextStyle(
                                                          color: Colors.white,
                                                          fontSize: 16,
                                                          backgroundColor:
                                                              Color.fromARGB(
                                                                  255, 7, 106, 11),
                                                        ),
                                                      )
                                                    : const Text('Inactive',
                                                        style: TextStyle(
                                                            color: Colors.white,
                                                            fontSize: 16,
                                                            backgroundColor:
                                                                Color.fromARGB(255,
                                                                    78, 61, 61))),
                                              ),
                                            ),
                                            DataCell(
                                              Container(
                                                padding: const EdgeInsets.symmetric(
                                                    horizontal: 8.0),
                                                child: Text(
                                                  DateFormat('dd-MM-yyyy').format(
                                                      premiseModels[i].created!),
                                                  style: defaultTextStyle(
                                                      color: Colors.white,
                                                      fontSize: 16),
                                                ),
                                              ),
                                            ),
                                          ],
                                        ),
                                    ],
                                  ),
                                ),
                              );
                            }
                            return const Center(child: CircularProgressIndicator());
                          },
                        ),

This is working for searching and filtering. But when I delete what I have searched it doesn't go back to the original list. Why is that? This is working for searching and filtering. But when I delete what I have searched it doesn't go back to the original list. Why is that? This is working for searching and filtering. But when I delete what I have searched it doesn't go back to the original list. Why is that? This is working for searching and filtering. But when I delete what I have searched it doesn't go back to the original list. Why is that?

1 Answer 1

0

Try calling always toList() method to your list allPremises:

if (query.isEmpty) {
    emit(CollectionSuccess<PremiseModel>(allPremises.toList()));
  } else {
    final filteredPremises = allPremises.toList().where((premise) {
      /// filter action
    });
  }

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.