Jonathon
Jonathon

Reputation: 1591

Can't pass data to constructor (flutter)

I'm trying to pass a list to a Stateful widget's constructor, but when adding the widget inside main.dart, it's not requiring any parameters.


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) {...

Adding Appointments() inside main.dart

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()...

Upvotes: 0

Views: 977

Answers (1)

Shayne Kelly II
Shayne Kelly II

Reputation: 143

If your Appointments widget requires a non-null list of clients, make it a required parameter in the constructor:

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

Then call it like this in main.dart:

Appointments(clients: _clients),

Upvotes: 1

Related Questions