gpt4 book ai didi

flutter - 要声明什么类型

转载 作者:行者123 更新时间:2023-12-03 03:57:23 25 4
gpt4 key购买 nike

所以有一个这样的课

class Base<T extends BaseClass> extends StatefulWidget

我想使用该类而不直接提供扩展BaseClass的Object,而是通过构造函数传递一些东西。
class Something extends StatelessWidget {
final /*what type I am supposed to give here*/ test;

const Something({Key key, this.test}) : super(key: key);

@override
Widget build(BuildContext context) {
return Base<test>();
}
}

我得到的错误警告是
'dynamic' doesn't extend 'BaseClass'.
Try using a type that is or is a subclass of 'BaseClass'.dart(type_argument_not_matching_bounds)

The name 'test' isn't a type so it can't be used as a type argument.
Try correcting the name to an existing type, or defining a type named 'test'.dart(non_type_as_type_argument)

最佳答案

您可以用“类型”声明,但是不能从代表该类的Type对象创建类的实例。

class Something<T extends BaseClass> extends StatelessWidget {
// final Type test;

// const Something({Key key, this.test}) : super(key: key);

@override
Widget build(BuildContext context) {
return Base<T>();
}
}

创建内容小部件:
class NewClass extends BaseClass {}

Widget x = Something<NewClass>();

关于flutter - 要声明什么类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59924915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com