gpt4 book ai didi

dart - Flutter beta 2更新后如何更新ListView

转载 作者:IT王子 更新时间:2023-10-29 07:14:09 26 4
gpt4 key购买 nike

下面的代码是原始代码的简化版本,我的目标是获得一个列表 block 列表:我可以在其中跟踪新创建的 ListTile 小部件的列表,我可以将其用作传递给 ListView 的参数.

class Notes extends StatefulWidget{

_NotesState createState() => new _NotesState();
}

class _NotesState extends State<Notes>{

List<ListTile> notes =[];

void addNewNote(){
setState((){
notes.add(new ListTile(title: new Text("Broccolli")));
}

@override
Widget build(BuildContext context){
return new Scaffold(
appBar: new AppBar(title: "NOTES"),
body: new Stack(
children: <Widget>[
// The notes
new Container(
child: new ListView(
children: new List.from(notes, growable: false),
)
),
// The add notes button
new FloatingActionButton(
tooltip: 'Add a note',
onPressed: addNewNote,
)
],
),
);
}

在引入 Dart 2 的最后一次 flutter 更新之前,这曾经工作得很好,但现在我收到以下消息:

type 'List<dynamic>' is not a subtype of type 'List<Widget>' where
List is from dart:core
List is from dart:core
Widget is from package:flutter/src/widgets/framework.dart

问题源于:new List.from(notes, growable: false)

我这样做的原因是因为当我只是将列表作为参数传递给 ListView.children flutter 不会注册更改并且不会显示列表的新元素,所以我只是创建一个新列表和我的问题得到解决。然而,这不再可行

最佳答案

您必须指定 List 的类型创建者 new List.from否则默认为 dynamic .

事实是,在强模式下,List<dynamic>不可分配给 List<whatever>了。所以你必须确保你的 List具有正确的类型。

在你的例子中,一个简单的 new List<Widget>.from(notes)会成功的

关于dart - Flutter beta 2更新后如何更新ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49844271/

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