Search code examples
flutterflutter-packagesflutter-android

How to show displayName of contact as text?


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