Home > database >  How to show displayName of contact as text?
How to show displayName of contact as text?

Time:02-28

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');

CodePudding user response:

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