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

标签 flutter dart

我正在尝试将列表传递给有状态小部件的构造函数,但是在 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/

相关文章:

json - 如何在 flutter 中从 url Json 数组中获取数据?

android - Flutter 中项目复用后的问题

flutter - 无法以健全的空安全运行,因为以下依赖项不支持空安全

android - 是否可以在凸起的按钮和文本字段之间创建连接?

dart - 如何在扩展方法中使用运算符?

flutter - 方法 'PermissionHandler' 没有为类定义

asynchronous - Dart 中的 future 队列

Dart 语言三角函数

android - 对齐小部件内部在 flutter 中按行展开

flutter更新1.17 android崩溃