在 Flutter 中将新的 ListTile 作为子项添加后,ListView 更新不会更新
ListView update doesn't update after new ListTile gets added as a children in Flutter
我有一个简单的 flutter 应用程序,它只显示 ListView 的 ListTiles...此 listView 的子项 属性 设置为名为“tiles”的列表...每当用户按下浮动操作按钮,然后将一个新的 ListTiles 对象添加到“tiles”,并将其包装在设置状态中... 所以我希望 UI 更新并向我显示新的更新列表视图更多 ListTiles...但这并没有发生...
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Home(),
);
}
}
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
List<ListTile> tiles = [];
@override
Widget build(BuildContext context) {
print('length of tiles: ${tiles.length}'); // prints the length of tiles correctly
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
tiles.add(ListTile(title: Text('Hello ${tiles.length+1}')));
});
},
),
body: ListView(children: tiles),
);
}
}
正如@LonelyWolf 所说:您应该使用 ListView.builder
并将字符串添加到您的列表而不是小部件
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
List<String> tiles = [];
@override
Widget build(BuildContext context) {
print('length of tiles: ${tiles.length}'); // prints the length of tiles correctly
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
tiles.add("${tiles.length+1}");
});
},
),
body: ListView.builder(
itemCount: tiles.length,
itemBuilder: (_, index){
return ListTile(
title: Text('Hello ${tiles[index]}'),
);
},
),
);
}
}
我有一个简单的 flutter 应用程序,它只显示 ListView 的 ListTiles...此 listView 的子项 属性 设置为名为“tiles”的列表...每当用户按下浮动操作按钮,然后将一个新的 ListTiles 对象添加到“tiles”,并将其包装在设置状态中... 所以我希望 UI 更新并向我显示新的更新列表视图更多 ListTiles...但这并没有发生...
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Home(),
);
}
}
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
List<ListTile> tiles = [];
@override
Widget build(BuildContext context) {
print('length of tiles: ${tiles.length}'); // prints the length of tiles correctly
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
tiles.add(ListTile(title: Text('Hello ${tiles.length+1}')));
});
},
),
body: ListView(children: tiles),
);
}
}
正如@LonelyWolf 所说:您应该使用 ListView.builder
并将字符串添加到您的列表而不是小部件
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
List<String> tiles = [];
@override
Widget build(BuildContext context) {
print('length of tiles: ${tiles.length}'); // prints the length of tiles correctly
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
tiles.add("${tiles.length+1}");
});
},
),
body: ListView.builder(
itemCount: tiles.length,
itemBuilder: (_, index){
return ListTile(
title: Text('Hello ${tiles[index]}'),
);
},
),
);
}
}