I was looking at the https://docs.flutter.dev/get-started/codelab tutorial for dart, and I noticed that they kept recalling their iterator. I wanted to keep the iterator around, and just continue to pull from it, so I worked up this:
class _FunPageState extends State<FunPage> {
final _things = <Widget>[];
late Iterable<Widget> _funPuller;
@override
void initState() {
super.initState();
_funPuller = generateFunThings();
}
Iterable<Widget> generateFunThings() sync*{
int i=1;
while (true) {
yield Text("$i");
i += 1;
}
}
@override
Widget build(BuildContext context) => ListView.builder(
padding: const EdgeInsets.all(8),
itemBuilder: (context, index) {
if (index.isOdd) {
return const Divider();
}
index = (index/2).floor();
if (index >= _things.length) {
_things.addAll(_funPuller.take(2));
}
return _things[index];
},
);
}
Except I noticed that this just generated the list 1,2,1,2,1,2,... rather than 1,2,3,4,5,... as I expected. Is there a way I can get the result of generateFunThings() to persist so I can continue calling it?