如何在不更改屏幕/页面的情况下快速传递数据?

时间:2019-08-28 05:54:40

标签: flutter

在一个屏幕中,我有另一个名为valueAbstractAll的屏幕的值。在同一屏幕上,我要调用该值。我用它来过滤CustomListView类中的列表。但是它显示错误“未定义名称”。如何解决?

class AbstractScreen extends StatefulWidget {

  final Spacecraft valueAbstractAll;

  AbstractScreen({Key key, this.valueAbstractAll}) : super(key: key);

  @override
  _AbstractScreenState createState() => _AbstractScreenState();
}
class _AbstractScreenState extends State<AbstractScreen> {
.....
}

class CustomListView extends StatelessWidget {


  final List<SpacecraftAbstract> spacecrafts;

  CustomListView(List<SpacecraftAbstract> spacecraftsAbstract,) :
        spacecrafts = spacecraftsAbstract.where((SpacecraftAbstract)
       => SpacecraftAbstract.id_conference == valueAbstractAll.id)
           .toList();

  Widget build(context) {
    return ListView.builder(
      itemCount: spacecrafts.length,
      itemBuilder: (context, int currentIndex) {
        return createViewItem(spacecrafts[currentIndex], context);
      },
    );
  } ..............

1 个答案:

答案 0 :(得分:0)

如果要在类B中获取类A的某些参数,则需要在类B中具有类A的实例。

您可以创建一个A对象,或将其与B构造函数一起传递,...

例如:

class AbstractScreen extends StatefulWidget {

  final Spacecraft valueAbstractAll;

  AbstractScreen({Key key, this.valueAbstractAll}) : super(key: key);

  @override
  _AbstractScreenState createState() => _AbstractScreenState();
}
class _AbstractScreenState extends State<AbstractScreen> {
.....
}

class CustomListView extends StatelessWidget {

//here I create the instance of AbstractScreen, you can initialize abstractScreen on constructor
AbstractScreen abstractScreen = new AbstractScreen();

  final List<SpacecraftAbstract> spacecrafts;

  CustomListView(List<SpacecraftAbstract> spacecraftsAbstract,) :
        spacecrafts = spacecraftsAbstract.where((SpacecraftAbstract)
       => SpacecraftAbstract.id_conference == abstractScreen.valueAbstractAll.id /*get valueAbstractAll of AbstractScreen instance*/ )
           .toList();

  Widget build(context) {
    return ListView.builder(
      itemCount: spacecrafts.length,
      itemBuilder: (context, int currentIndex) {
        return createViewItem(spacecrafts[currentIndex], context);
      },
    );
  } ..............