QuisApp / flutter_contacts

MIT License
83 stars 138 forks source link

conact.name.first contains the whole contact full name and contact.name.last is always null #153

Closed Valent1 closed 3 months ago

Valent1 commented 5 months ago

I just noticed that my contacts don't have any last name after import. The first name contains the whole full name, and the last name is null. I put a log on first and last and this is the output (note that in this method, firstName and lastName are passed as contact?.name.first and contact?.name.last.

  String _getInitials(String? firstName, String? lastName) {
    StringBuffer initials = StringBuffer();

    if (firstName != null && firstName.trim().isNotEmpty) {
      initials.write(firstName.trim()[0].toUpperCase());
    }

    if (lastName != null && lastName.trim().isNotEmpty) {
      initials.write(lastName.trim()[0].toUpperCase());
    }

    log.shout("FirstName: $firstName");
    log.shout("LastName: $lastName");

    return initials.toString();
  }

Sample of log output: ... [Log] 2024-04-02 22:28:02.853410: SHOUT: LastName: null [Log] 2024-04-02 22:28:02.911795: SHOUT: FirstName: Alessandra Stefanini [Log] 2024-04-02 22:28:02.911894: SHOUT: LastName: null [Log] 2024-04-02 22:28:14.073969: SHOUT: FirstName: Alessandra Stefanini [Log] 2024-04-02 22:28:14.074276: SHOUT: LastName: null [Log] 2024-04-02 22:28:14.098594: SHOUT: FirstName: Alessandro Alabastri [Log] 2024-04-02 22:28:14.098744: SHOUT: LastName: null [Log] 2024-04-02 22:28:14.136106: SHOUT: FirstName: Alessandro Bellan [Log] 2024-04-02 22:28:14.136235: SHOUT: LastName: null ...

Valent1 commented 5 months ago

➜ airplaza git:(main) ✗ flutter doctor -v [✓] Flutter (Channel stable, 3.19.5, on macOS 14.4.1 23E224 darwin-arm64, locale en-US) • Flutter version 3.19.5 on channel stable at /Users/valentino/Flutter/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 300451adae (6 days ago), 2024-03-27 21:54:07 -0500 • Engine revision e76c956498 • Dart version 3.3.3 • DevTools version 2.31.1

[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0-rc1) • Android SDK at /Users/valentino/Library/Android/sdk • Platform android-34, build-tools 35.0.0-rc1 • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874) • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.3) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 15E204a • CocoaPods version 1.15.2

[✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2023.2) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874)

[✓] VS Code (version 1.87.2) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.86.0

[✓] Connected device (3 available) • Valentino’s iPhone (mobile) • 00008130-0018715E26B8001C • ios • iOS 17.3.1 21D61 • macOS (desktop) • macos • darwin-arm64 • macOS 14.4.1 23E224 darwin-arm64 • Chrome (web) • chrome • web-javascript • Google Chrome 123.0.6312.105

[✓] Network resources • All expected network resources are available.

• No issues found! ➜ airplaza git:(main) ✗

joachim-quis commented 5 months ago

Hi @Valent1

This is probably because that's how your contact was saved. Many contact apps don't automatically split first and last name when you add a contact. You can use an app like Quis (http://quis.co) to change that :)

If your contact shows as first name and last name properly separated on the default contact app, feel free to reopen this thread.

Valent1 commented 5 months ago

if it was that trivial, I wouldn't have posted this.

All my contacts are saved with First name and Last name, they are saved in Google Contacts and I sync them with my iPhone. I access them via Google Contacts or the native Apple Contacts/Phone apps, no problem.

However, as I said in my original post, contacts are imported in my flutter app with a null last name via flutter_contacts.

Would you mind trying to replicate this on your physical iOS device? Just sync your contacts and then print some out, the ones that you know are saved correctly with first and last name.

Valent1 commented 5 months ago

How do I reopen this?

joachim-quis commented 5 months ago

I have the same setup as you and never had this problem. So I'm not sure what's going on. You can see from this line that name.first is a straight copy of c.givenName from the native iOS contact, and there's not much else we can do if for some reason that one contains first+last.

Could you try logging the whole contact name? And can you also try logging what CNContact looks like at this point?

joachim-quis commented 3 months ago

With no additional info, it's difficult to help. Please re-open with more details (code, flutter doctor -v, etc).