gpt4 book ai didi

android-studio - 数据更新后 flutter ListView 未更新

转载 作者:行者123 更新时间:2023-12-03 03:07:55 24 4
gpt4 key购买 nike

我在bottomSheet中有一个ListView,它是使用元素数组构建的。目前我在那里有一个项目“空”,然后是 .clear() ed 并在异步数据库调用后填充。

变量更新是正确的,我尝试使用setState((){})但 ListView 根本没有更新。我需要关闭bottomSheet,重新打开它,然后ListView 就会有正确的项目。

我需要打电话setState吗?或者是否需要标记bottomSheet builder来更新?

主 ListView 部分:

 @override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('My Map'),
backgroundColor: Colors.green[700],
),

//Put in a stack widget so can layer other widgets on top of map widget
body: Stack(
children: <Widget>[
GoogleMap(
mapType: _currentMapType,
markers: _markers,
onMapCreated: _onMapCreated,
onCameraMove: _onCameraMove,
initialCameraPosition: CameraPosition(
target: _center,
zoom: 11.0,
),
),
Padding(
padding: const EdgeInsets.all(16.0),
child: Align(
alignment: Alignment.bottomCenter,
child: Row(mainAxisSize: MainAxisSize.min, children: <Widget>[
SizedBox(width: 16.0),

Builder(
builder: (context) => FloatingActionButton(
...
),

),

SizedBox(width: 16.0),

FloatingActionButton(
...
),

SizedBox(width: 16.0),

Builder(
builder: (context) => FloatingActionButton(
child: Icon(Icons.file_download, size: 36.0),
backgroundColor: Colors.green,
onPressed: () {
showBottomSheet(
context: context,
builder: (context) {
return ListView(
padding: EdgeInsets.all(15.0),
children: <Widget>[

...

Divider(),

ListTile(
title: Text("Remote JSON Download"),
trailing:
Icon(Icons.refresh),
selected: true,
onTap: _OnPressedReloadJSON, <-------------
),

Divider(),

Container(
height: 150.0,
child: ListView.builder( <-------------
scrollDirection: Axis.horizontal,

itemCount : testList.length,
itemBuilder: (BuildContext context, int index) {
return Container(
padding: EdgeInsets.all(5.0),
margin: EdgeInsets.all(5.0),
width: 150.0,
color: Colors.red,
child: Text(testList[index]),
);
},

),
),

异步获取:
class _MyAppState extends State<MyApp> {

...

_OnPressedReloadJSON() {
fetchJSONAndMakeListTile();
}

...

List<String> testList= ["empty"];

Future<http.Response> fetchJSONAndMakeListTile() async {
final response = await http.get('https://..... file.json');

// If the server did return a 200 OK response, then parse the JSON.
if (response.statusCode == 200) {
List<db_manager.MyObject> myObjects = (json.decode(response.body) as List)
.map((e) => db_manager.MyObject.fromJson(e))
.toList();

testList.clear();
myObjects.forEach((db_manager.MyObject al) {
testList.add(al.code);
print("debug:"+al.code+" - "+al.name); <------------- prints correctly
});

//TODO Does this even work?
//Trigger update of state - ie redraw/reload UI elements
setState(() {});

} else {
// If the server did not return a 200 OK response, then throw an exception.
print(response);
throw Exception('Failed to load json');
}
}

更新:

我已将 BottomSheet 构建器抽象为另一个类(根据另一个答案)作为它自己的 StatefulWidget,但我似乎无法访问 void onPress()来自我的主要 dart 文件的方法。如果BottomSheet 创建/构建器在这个单独的dart 文件中,我如何调用它来构建然后使用 async 更新其状态调用更新 ListView 内容列表?

BottomSheetWidget.dart
class BottomSheetDatabases extends StatefulWidget {
@override
_BottomSheetDatabases createState() => _BottomSheetDatabases();
}

class _BottomSheetDatabases extends State<BottomSheetDatabases> {

void _onpress() {

}

void loadMe() {

}

List<String> testList= ["empty"];

@override
Widget build(BuildContext context) {
return BottomSheet(

builder: (context) {
return ListView(
padding: EdgeInsets.all(15.0),
children: <Widget>[
ListTile(
...

),

Divider(),

Container(
height: 150.0,
child: ListView.builder(
scrollDirection: Axis.horizontal,

itemCount: testList.length,
itemBuilder: (BuildContext context, int index) {
return Container(
key: UniqueKey(),
//TODO ??
padding: EdgeInsets.all(5.0),
margin: EdgeInsets.all(5.0),
width: 150.0,
color: Colors.red,
child: Text(testList[index]),
);
},

),
//),
//),
),

...

Main.dart :
  void _loadSheetDatabases() {
BottomSheetWidget bottomSheetwidget = BottomSheetWidget();
bottomSheetwidget.loadMe();
}

最佳答案

在我看来,一个 Key缺少 ListView.Builder 返回的小部件,请尝试放置 UniqueKey()或 ValueKey 到容器或文本中:

Container(
key: UniqueKey(),
padding: EdgeInsets.all(5.0),
margin: EdgeInsets.all(5.0),
width: 150.0,
color: Colors.red,
child: Text(testList[index]),
);

关于android-studio - 数据更新后 flutter ListView 未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60304852/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com