0

How to fix code my code flutter and use plugin

     filterContacts() {
        setState(() {
          List<Contact> _contacts = [];
              _contacts.addAll(contacts);
          if (searchController.text.isNotEmpty) {
            _contacts.retainWhere(
              (contact) {
                String searchTerm = searchController.text.toLowerCase().trim();
                String searchTermFlatten = flattenPhoneNumber(searchTerm);
                String contactName = contact.displayName.toString().toLowerCase();
                bool nameMatches = contactName.contains(searchTerm);
                if (nameMatches == true) {
                  return true;
                }
                if (searchTermFlatten.isEmpty) {
                  return false;
                }
    
                var phone = contact.phones.firstWhere((phn) {
                  String phnFlattened = flattenPhoneNumber(phn);
                  return phnFlattened.contains(searchTermFlatten);
                }, orElse: () => null);
    
                return phone != null;
              },
            );
                contactsFiltered = _contacts;
          }
        });
      }

Flutter code How to fix code my code flutter and use plugin contacts_service, this image is about a problem

Mojtaba Ghiasi
  • 823
  • 6
  • 16

2 Answers2

0

contact.phones can be null, in this you need to check its value 1st then proceed,

you can use contact.phones?.firstWhere to handle this situation or

If you're sure it will have value, you can also do contact.phones!.firstWhere but I don't recommend this. You don't need to use orElse you want to pass null,

   Item? phone = contact.phones?.firstWhere((phn) {
              String phnFlattened = flattenPhoneNumber(phn);
              return phnFlattened.contains(searchTermFlatten);
            },  );

You can learn more about ?. !...

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
0

[how to fix now] error code not complete

  filterContacts() {
    setState(() {
      List<Contact> _contacts = [];

      _contacts.addAll(contacts);
      if (searchController.text.isNotEmpty) {
        _contacts.retainWhere(
          (contact) {
            String searchTerm = searchController.text.toLowerCase().trim();
            String searchTermFlatten = flattenPhoneNumber(searchTerm);
            String contactName = contact.displayName.toString().toLowerCase();
            bool nameMatches = contactName.contains(searchTerm);
            if (nameMatches == true) {
              return true;
            }

            if (searchTermFlatten.isEmpty) {
              return false;
            }

            Item? phone = contact.phones?.firstWhere((phn) {
              String phnFlattened = flattenPhoneNumber(phn);
              return phnFlattened.contains(searchTermFlatten);
            }, );

            return phone != null;
          },
        );

        contactsFiltered = _contacts;
      }
    });
  }

  • Are there any changes beside my answer ? you can check [this](https://stackoverflow.com/help/someone-answers#:~:text=Choose%20one%20answer%20that%20you,the%20answer%2C%20at%20any%20time.) – Md. Yeasin Sheikh Nov 25 '21 at 09:49