I have a function in which I add StreamBuilder
(s) into a List, then I load these StreamBuilder
(s) inside a Column
, this approach is really memory consuming as all of the List
is loaded at once, is there a way to do this in another way?
My showList
function:
showList() {
List<Widget> theList= [];
for (CountryModel country in _allCountries) {
theList.add(StreamBuilder(
stream:FirebaseFirestore.instance.collection('user').doc(country.Id).snapshots(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
xModel user = UserModel.fromDoc(snapshot.data);
return CountryWidget(
country: country,
viewer: user.id as String,);
} else {
return SizedBox.shrink();
}
}));
}
return theList;
}
In my Scaffold I have a Column where I use the spread operator to load a list into children[].
Column(
children: <Widget>[
...showList()
],
),
CodePudding user response:
Try the following code:
ListView.builder(
itemCount: _allCountries.length _anotherList.length,
itemBuilder: (context, index) {
if (index >= _allCountries != true) {
final CountryModel country = _allCountries[index];
return StreamBuilder(
stream: FirebaseFirestore.instance.collection('user').doc(country.Id).snapshots(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
xModel user = UserModel.fromDoc(snapshot.data);
return CountryWidget(
country: country,
viewer: user.id as String,
);
} else {
return SizedBox.shrink();
}
}
final item = _anotherList[index];
return widget;
},
);
},
),