I am trying to create a listview screen, but there is an error in the 'myChildren' part. What could be the cause?
CodePudding user response:
Either move myChildren
to _MyHomePageState
class or replace myChildren
with widget.myChildren
in build
method.
Also everytime when build
method calls you add 50 text widgets to myChildren
. Is this what you need?
CodePudding user response:
You can also use it like this.
import 'package:flutter/material.dart';
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<Widget> myChildren = [];
@override
void initState() {
WidgetsBinding.instance.addPostFrameCallback((_) {
for(var i = 0; i<50;i ){
myChildren.add(const Text('Hellow World'));
}
setState(() {});
});
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Example'),
),
body: myChildren.isEmpty
? const SizedBox()
: ListView.builder(
itemCount: myChildren.length,
itemBuilder: (BuildContext context, int index) {
return myChildren[index];
},
),
);
}
}