dart的泛型和动态有什么区别? [英] What is the difference between generics and dynamic in dart?
问题描述
让我介绍出现此问题的错误。 (在此处详细说明)
Let me introduce the errors why I got this issue. (Detail here)
type '(String, String) => bool' is not a subtype of type '(dynamic, String) => bool'
这是来自 material_search 和解决方案是:
- _filter(T v, String c) {
+ _filter(dynamic v, String c) {
- onSelect: (T value) => Navigator.of(context).pop(value),
+ OnSelect: (dynamic value) => Navigator.of(context).pop(value),
更改所有泛型类型 T
变成动态的,问题似乎在飞镖2出现时发生。
Changing all of generics type T
to dynamic, and The issue seems to be happened when dart 2 is appearing.
所以,我在这里遇到了这些问题,
So, I got these question here,
- 飞镖中的泛型和动态有什么区别?
- 仅适用于仿制药的限制是什么?在以上问题中,这仅适用于动态。
- What is the difference between generics and dynamic in dart?
- What's the limit that only works with generics or on the other hand? In the above issue, this is only works with dynamic.
编辑:
让我提供一个简单的例子来使问题更清楚: p>
用遗传学定义一个类
Let me provide a simple example to make question more clear:
typedef bool GeneticFunction<T>(T geneticData, String key);
class Component<T> extends StatefulWidget {
Component({this.geneticFunc});
final GeneticFunction<T> geneticFunc;
@override
_componentState<T> createState() => _componentState<T>();
}
一种方法在下面工作
#1
One of the method is working below
#1 Component<CoolType>(
geneticFunc: (CoolType cool, String keyword){
return false;
},
);
#2
#2
Component<CoolType>(
geneticFunc: (dynamic cool, String keyword){
return false;
},
);
#2 的答案有效,这意味着我甚至不需要通用,只需动态即可。如果您使用#1 ,有时运行时甚至没有错误,并且可能一整天都呆在那里。
The answer #2 is working, and It means I don't even need generic, just go dynamic. If you use #1, there is sometimes even no error in runtime, and you might stuck at there whole day.
讨论此处,表示 T 在运行时总是动态,因此#2 是唯一选择。
There is an official discussion here, said that T would always be dynamic in runtime, so #2 is the only choosen.
最后,我不知道何时使用泛型,并且由于上面的结果,现在似乎总是使用动态。
In the conclusion, I have no idea when to use generic, and seems to be always use dynamic now, because of the result above.
推荐答案
很抱歉,我的问题长时间拖延,此问题的真正问题是在StatefulWidget中使用泛型的正确方法是什么?
Sorry for the long delay for my question, the real problem for this issue is What is the correct way to use generic with StatefulWidget?
让我们看一下原始代码:
Let's see the original code:
class Component<T> extends StatefulWidget {
Component({this.data, this.geneticFunc});
final T data;
final GeneticFunction<T> geneticFunc;
void test()
{
var testFunc = geneticFunc;
testFunc(data, "123");
}
@override
_ComponentState<T> createState() {
return _ComponentState<T>();
}
}
class _ComponentState<T> extends State<Component>
{
@override
void initState() {
print("test one");
var a = widget.geneticFunc;
print("test two");
super.initState();
}
@override
Widget build(BuildContext context) {
return Container(width: 20, height: 20,);
}
}
猜猜是什么?只要记住在扩展泛型类时添加< T>
,问题就会变得很复杂。
Guess what? just remember to add a <T>
when extending a generic class, and the problem sloved.
class _ComponentState<T> extends State<Component<T>>
所以问题 type‘(String,String)=> bool'不是类型为'(dynamic,String)=>的子类型。 bool'
再也不会打扰我了。
So the issue type '(String, String) => bool' is not a subtype of type '(dynamic, String) => bool'
will never bother me again.
下面的代码应该可以正常工作了。
And the following code should work now.
Component<CoolType>(
geneticFunc: (CoolType a, String key){
return false;
},
)
这篇关于dart的泛型和动态有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!