The argument type 'String?' can't be assigned to the parameter type 'String & Method can't be unconditionally invoked because to receiver can be null

Issue

I’m new to Flutter and I’ve been trying to use the package contacts_service to be able to have a list with names & numbers from your contacts.
Below is the code of attempt, however "title: Text(contact.displayName)" gives me the error displayName error and "subtitle: Text(contact.phones.elementAt(0).value gives me the error elementAt error.
From what I understand this is due to null safety, but I’m not quite sure where or how to check for null in my code. Do I do it inside the scaffold or before I build it?
Thank you

import 'package:contacts_service/contacts_service.dart';
import 'package:flutter/material.dart';

class Contacts extends StatefulWidget {
  @override
  _Contacts createState() => _Contacts();
}
class _Contacts extends State<Contacts>{
  List<Contact> contacts =[];
  @override
  void initState() {
    super.initState();
    getAllContacts();
  }

  getAllContacts() async{
    List<Contact> _contacts = (await ContactsService.getContacts(withThumbnails: false)).toList();
    setState(() {
      contacts = _contacts;
    });
  }
  @override
  Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(title: Text('Contact info'
        ),),
        body: Container(
          padding: EdgeInsets.all(20),
          child: Column(
            children: <Widget>[ Text ('test'),
          ListView.builder(
                shrinkWrap: true,
                itemCount: contacts.length,
                itemBuilder: (context, index) {
                  Contact contact = contacts[index];
                  return ListTile(
                    title: Text(contact.displayName),
                    subtitle: Text(
                       contact.phones.elementAt(0).value
                    ),
                  );
                },
              )
            ],
          ),
        ),
      );
    }
  }

Solution

import 'package:contacts_service/contacts_service.dart';
 import 'package:flutter/material.dart';

 class Contacts extends StatefulWidget {
  @override
  _Contacts createState() => _Contacts();
  }
class _Contacts extends State<Contacts>{
 List<Contact> contacts =[];
  @override
 void initState() {
super.initState();
getAllContacts();
   }

  getAllContacts() async{
List<Contact> _contacts = (await ContactsService.getContacts(withThumbnails: 
    false)).toList();
setState(() {
  contacts = _contacts;
});
    }

      @override
      Widget build(BuildContext context) {
       return Scaffold(
    appBar: AppBar(title: Text('Contact info'
    ),),
    body: Container(
      padding: EdgeInsets.all(20),
      child: Column(
        children: <Widget>[ Text ('test'),
      ListView.builder(
            shrinkWrap: true,
            itemCount: contacts.length,
            itemBuilder: (context, index) {
             
              return ListTile(
                title: Text("${contact.displayName}"),
                subtitle: Text(
                   "${contact.phones.elementAt(0).value}"
                ),
              );
            },
          )
        ],
      ),
    ),
    );
    }
  }

TRY THIS

Answered By – Awais Rehman

Answer Checked By – Clifford M. (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.