处理动态加载列表 Andriod 飘红
- 空安全问题
void main () => runApp(MyApp(
items: List<String>.generate(100, (i) => "Item $i")
));
class MyApp extends StatelessWidget{
final List<String> items;
MyApp({Key? key,required this.items}):super(key:key);
@override
Widget build(BuildContext context ){
return MaterialApp(
title:'ListView widget',
home:Scaffold(
body: ListView.builder(
itemCount:items.length,
itemBuilder:(context,index){
return ListTile(
title: Text('${items[index]}'),
);
}
)
),
);
}
}
- 2.0后 必填参数: @required => required
void main () => runApp(MyApp(
items: List<String>.generate(100, (i) => "Item $i")
));
class MyApp extends StatelessWidget{
final List<String> items;
MyApp({Key? key, required this.items}):super(key:key);
@override
Widget build(BuildContext context ){
return MaterialApp(
title:'ListView widget',
home:Scaffold(
body: ListView.builder(
itemCount:items.length,
itemBuilder:(context,index){
return ListTile(
title: Text('${items[index]}'),
);
}
)
),
);
}
}
|