gpt4 book ai didi

flutter - 无法将数据传递给构造函数(抖动)

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

我正在尝试将列表传递给有状态小部件的构造函数,但是在 main.dart 中添加小部件时,它不需要任何参数。


class Appointments extends StatefulWidget {

List clients;

Appointments({Key key, this.clients}): super(key: key);

@override
State<StatefulWidget> createState() {
return AppointmentState();
}
}

class AppointmentState extends State<Appointments> {
@override
Widget build (BuildContext context) {
return Container(
child: Expanded(
child: ListView.builder(
itemCount: widget.clients.length,
itemBuilder: (context, index) {...

在 main.dart 中添加 Appointments()

class MyAppState extends State<MyApp> {
List _clients = ["James Doe", "Beth Oliver", "Martha Dixon", "Peter Kay"];

@override
Widget build (BuildContext context) {
return MaterialApp(
title: "MyApp",
home: Scaffold (
appBar: AppBar(
title: Text("Your Appointments")
),
body: Column(
children: [
Align(
alignment: AlignmentDirectional.center,
child: Text("Your Appointments"),
),
Appointments()...

最佳答案

如果您的Appointments 小部件需要一个非空的客户列表,请将其作为构造函数中的必需参数:

约会({Key key, @required this.clients}): super(key: key);

然后在main.dart中这样调用:

约会(客户:_clients),

关于flutter - 无法将数据传递给构造函数(抖动),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56997764/

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