1

I have two functions named getAllContacts() which will get all the contacts from mobile and upDateAllContacts() which creates widgets using contact numbers and append it to list named registerdUsers = [] and used the registedUser widget in column. The problem is widgets are no visible unless i navigate to another screen and come back.

List registeredUsers = <Widget>[]; class ...{ ... @override initState() { getAllContacts() //It greps all the contacts from mobile upDateAllContacts()//contacts to create widgets which goes in to registeredUsers list super.initState(); } @override Widget build(BuildContext context) { ... column { children :registeredUsers, } ... } } 

2 Answers 2

0

Make you function getAllContacts return a Future and use FutureBuilder to render widgets. Whenever that future's state changes or an error occurs, FutureBuilder will call it's build callback that returns appropriate widgets.

The code will look something like this:

FutureBuilder( // Assuming loadAllContacts returns Future<Contacts> future: loadAllContacts(), // This callback will be called whenever future's state changes builder: (BuildContext context, AsyncSnapshot<Contacts> snapshot) { if (snapshot.hasData) { return ListView(…); } else if (snapshot.hasError) { return Text('Failed to load the contacts'); } else { return Text('Loading the contacts'); } }, ), 
Sign up to request clarification or add additional context in comments.

Comments

0

Make getAllContacts a streamed event and then use StreamBuilder -> this will always have the latest updates to your contacts so you won't need to explicitly update them (no more upDateAllContacts).

You just need to ensure that your database query/grep returns a streamed event.

For example, Firebase allows easy realtime updates. Using Firebase realtime database a stream is returned as:

FirebaseDatabase.instance.reference().child('$refName').onValue

There are quite a few threads about using stream builder - here is one for Firebase using Firestore

If the issue then becomes about where you get your data from and how to make it a stream, then this thread asks a similar question and has no answers yet, haha.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.