将参数传递给initState

时间:2019-03-06 10:42:51

标签: dart flutter widget stateful

看下面的代码-小部件以获取数据并显示在列表中:

class _MyEventsFragmentState extends State <MyEventsFragment>{

  var events;

  @override
  initState(){
    super.initState();
    events = fetchEvents(true);
  }

  @override
  Widget build(BuildContext context) {
    return new Center(
        child: FutureBuilder<EventsResponse>(
          future: events,
          builder: (context, snapshot) {
            if (snapshot.connectionState == ConnectionState.done) {
              if (snapshot.hasError) {
                helpers.logout(context, Strings.msg_session_expired);
                return CircularProgressIndicator();
              }
              return new Container(color: Colors.white,
                  child: new ListControl().build(snapshot));
            }
            return CircularProgressIndicator();
          },
        )
    );
  }
}

fetchEvent方法具有用于指示我需要提取哪些事件的参数。如果设置为true,则-我的事件;如果设置为false-将返回所有事件。上面的代码加载了我的事件,在fetchEvents内调用了initState来避免不必要的数据重新加载。

要获取所有事件,我定义了另一个类:

class EventsFragment extends StatefulWidget {

  @override
  _EventsFragmentState createState() => new _EventsFragmentState();
}

class _EventsFragmentState extends State <EventsFragment>{

  var events;

  @override
  initState(){
    super.initState();
    events = fetchEvents(false);
  }

  @override
  Widget build(BuildContext context) {
    return new Center(
        child: FutureBuilder<EventsResponse>(
          future: events,
          builder: (context, snapshot) {
            if (snapshot.connectionState == ConnectionState.done) {
              if (snapshot.hasError) {
                helpers.logout(context, Strings.msg_session_expired);
                return CircularProgressIndicator();
              }
              return new Container(color: Colors.white,
                  child: new ListControl().build(snapshot));
            }
            return CircularProgressIndicator();
          },
        )
    );
  }
}

但这是非常愚蠢的解决方案,因为代码几乎相同。因此,我尝试传递布尔值来指示要加载的事件,诸如此类:

@override
  initState(){
    super.initState();
    events = fetchEvents(isMyEvents);
  }

isMyEvents应该从EventsFragment构造函数中获取。但是,在initState内部将无法使用它。何正确地通过?我可以在build覆盖内访问它,但不能在initState内访问它。如何正确传递它并确保每次创建小部件实例时都会刷新它?

[编辑]

这就是我解决问题的方式(似乎很好):

class EventsFragment extends StatefulWidget {
  const EventsFragment({Key key, this.isMyEvent}) : super(key: key);

  final bool isMyEvent;

  @override
  _EventsFragmentState createState() => new _EventsFragmentState();
}

class _EventsFragmentState extends State <EventsFragment>{

  var events;

  @override
  initState(){
    super.initState();
    events = fetchEvents(widget.isMyEvent);
  }

  @override
  void didUpdateWidget(EventsFragment oldWidget) {

    if(oldWidget.isMyEvent != widget.isMyEvent)
      events = fetchEvents(widget.isMyEvent);

    super.didUpdateWidget(oldWidget);
  }

  @override
  Widget build(BuildContext context) {
    return new Center(
        child: FutureBuilder<EventsResponse>(
          future: events,
          builder: (context, snapshot) {
            if (snapshot.connectionState == ConnectionState.done) {
              if (snapshot.hasError) {
                helpers.logout(context, Strings.msg_session_expired);
                return CircularProgressIndicator();
              }
              return new Container(color: Colors.white,
                  child: new ListControl().build(snapshot));
            }
            return CircularProgressIndicator();
          },
        )
    );
  }
}

1 个答案:

答案 0 :(得分:2)

将此类参数传递给StatefulWidget子类,并改用该字段

class Foo extends StatefulWidget {
  const Foo({Key key, this.isMyEvent}) : super(key: key);

  final bool isMyEvent;

  @override
  _FooState createState() => _FooState();
}

class _FooState extends State<Foo> {
  @override
  void initState() {
    super.initState();
    print(widget.isMyEvent);
  }

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

    );
  }
}