Home > Software engineering >  my code not complete for where contact number
my code not complete for where contact number

Time:11-25

[Flutter code][1] 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 [1]: https://i.stack.imgur.com/LH80g.jpg

CodePudding user response:

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 ?. !...

CodePudding user response:

[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;
      }
    });
  }

  • Related