Flutter 如何在flatter(遗传类型)中将任何类对象传递给有状态类
我有几个有状态类,如收入、支出、借记、事件等,每个类都有自己的属性集(int、double、string)。我还有另一个类,我想在所有有状态类中共享,因为代码类似。我想创建一个在所有类中通用的类,而不是为收入、费用等编写不同的类来完成特定的任务 下面是我的公共类的部分代码Flutter 如何在flatter(遗传类型)中将任何类对象传递给有状态类,flutter,dart,Flutter,Dart,我有几个有状态类,如收入、支出、借记、事件等,每个类都有自己的属性集(int、double、string)。我还有另一个类,我想在所有有状态类中共享,因为代码类似。我想创建一个在所有类中通用的类,而不是为收入、费用等编写不同的类来完成特定的任务 下面是我的公共类的部分代码 class CheckBoxAlertDialog extends StatefulWidget { CheckBoxAlertDialog({ Key key, this.anyobjectype,
class CheckBoxAlertDialog extends StatefulWidget {
CheckBoxAlertDialog({
Key key,
this.anyobjectype,
this.database
}) : super(key: key);
final T anyobjectype;
final AppDatabase database;
@override
CheckBoxAletDialogState createState() {
return new CheckBoxAletDialogState();
}
}
class CheckBoxAletDialogState extends State<CheckBoxAlertDialog> {
@override
Widget build(BuildContext context) {
If(anyobjectype is type income)
print(anyobjectype.amount) //assume income class has type double income
If(anyobjectype is type expense)
print(anyobjectype.paid_amount) //assume expense class has type double paid_amount
If(anyobjectype is type event)
print(anyobjectype.event_date) //assume event class has type date event_date
//and so on
}
}
基本上,我希望能够将任何类对象传递给CheckBoxAlertDialog,并能够在CheckBoxAlertDialog类中访问该类的数据类型。请参见CheckBoxAlertDialog中的生成函数我听说基因数据类型可以帮上忙,但我对这一点还不熟悉。有人能告诉我如何将任何类对象传递给CheckBoxAlertDialog,并能够像上面的示例那样访问CheckBoxAlertDialog中的类数据类型吗?提前感谢尝试使用
InheritedWidget
。它允许您的子窗口小部件访问父类的成员。查阅以下文件:
这个网站也很有帮助:
income = new income();
CheckBoxAlertDialog(anyobjectype : income, database: database,);
expense = new expense();
CheckBoxAlertDialog(anyobjectype : expense, database: database,);
debit = new debit();
CheckBoxAlertDialog(anyobjectype : debit, database: database,);