How to show displayName of contact as text?

Issue

I want to pick a random number from contact list and show it as Text. Below is what I came up with but when I insert showNext() somewhere on main.dart as text, I get Closure: () => Future from Function ‘showNext’: static.() instead of a number. How do I show a number?

Future<void> showNext() async {
  var status = await Permission.contacts.status;
    if (status.isGranted) {
  var contacts = await ContactsService.getContacts;
  var list = contacts;
    list.shuffle();
  randomNum= (list.first.phones?.first.value ?? '');
  Text('$randomNum');

Solution

  Future<Widget> showNext() async {
  var status = await Permission.contacts.status;
  if (status.isGranted) {
     final Iterable<Contact> contacts = (await ContactsService.getContacts(withThumbnails: false));
     var list = contacts.toList();
     list=list.shuffle();
     randomNum= (list.first.phones?.first.value ?? '');
     return Text('$randomNum');
  }

when you want use:

FutureBuilder<String>(
          future: showNext(),
          builder: (context, snapShot) {
            if (!snapShot.hasData) {
              return Container();
            }
            return snapShot.data;
          },
        )

Answered By – user3257386

Answer Checked By – Marilyn (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *