英文:
How to create a generic callback function in Flutter?
问题
以下是代码的翻译部分:
我想要为Flutter中的一个按钮创建一个通用的回调函数。我希望它接受泛型类型作为参数,以便我可以在不同类型上使用它。我尝试创建它如下。
class GenericButtonWidget extends StatelessWidget {
const GenericButtonWidget({Key? key, this.save, this.delete}) : super(key: key);
final Function<T>(T)? save;
final Function<T>(T)? delete;
@override
Widget build(BuildContext context) {
return Container(
child: Column(
children: [
InkWell(
onTap: () => save,
child: Text('Save'),
),
InkWell(
onTap: save, // 这里出现错误
child: Text('Delete'),
),
],
),
);
}
}
错误信息为:参数类型'dynamic Function(dynamic)?'无法分配给参数类型'void Function()?'
我需要指导,因为我不知道我是对还是错。谢谢。
英文:
I wanted to create a generic call back function for a button in Flutter. I want that it takes generic type as argument so that I can use it with different types. I tried to create is like this.
class GenericButtonWidget extends StatelessWidget {
const GenericButtonWidget({Key? key, this.save, this.delete}) : super(key: key);
final Function<T>(T)? save;
final Function<T>(T)? delete;
@override
Widget build(BuildContext context) {
return Container(
child: Column(
children: [
InkWell(
onTap: () => save,
child: Text('Save'),
),
InkWell(
onTap: save, //error here
child: Text('Delete'),
),
],
),
);
}
}
}
Error is : The argument type 'dynamic Function(dynamic)?' can't be assigned to the parameter type 'void Function()?'
I need guidance because I do not know, I am doing it right or wrong. Thanks.
答案1
得分: 1
以下是翻译好的部分:
What you are trying to do does not work. The generic variable needs to decide on a type and continue with that. What you are trying to do is to be able to assign to it different types in the future, which is not how generics work. The dynamic
type is what you are looking for as suggested in the other answer here.
I'm not sure why you want this. Having strong types is preferable to dynamic types as it reduces bugs and gives you peace of mind.
Having said that, this works
Function<T>(T a) save = <T>(T a) {
print(a);
return a;
};
void main() {
save(5);
}
but this does not, because of the same error you mentioned.
Function<T>(T a) save = (int a) {
print(a);
return a;
};
void main() {
save(5);
}
So the caller in your example could potentially be generic as well, and could decide on the generic parameter of save here that way, maybe.
英文:
What you are trying to do does not work. The generic variable needs to decide on a type and continue with that. What you are trying to do is to be able to assign to it different types in the future, which is not how generics work. The dynamic
type is what you are looking for as suggested in the other answer here.
I'm not sure why you want this. Having strong types is preferable to dynamic types as it reduces bugs and gives you piece of mind.
Having said that, this works
Function<T>(T a) save = <T>(T a) {
print(a);
return a;
};
void main() {
save(5);
}
but this does not, because of the same error you mentioned.
Function<T>(T a) save = (int a) {
print(a);
return a;
};
void main() {
save(5);
}
So the caller in your example could potentially be generic as well, and could decide on the generic parameter of save here that way, maybe.
答案2
得分: 1
你可以使用 call
函数。例如:
class GenericButtonWidget<T> extends StatelessWidget {
const GenericButtonWidget({Key? key, this.save, this.delete}) : super(key: key);
final void Function(T)? save;
final void Function(T)? delete;
@override
Widget build(BuildContext context) {
return Container(
child: Column(
children: [
InkWell(
onTap: () => save?.call(0 as T),
child: Text('Save'),
),
InkWell(
onTap: () => delete?.call(0 as T),
child: Text('Delete'),
),
],
),
);
}
}
这是你提供的代码的翻译部分。
英文:
You can use call
function. Like:
class GenericButtonWidget<T> extends StatelessWidget {
const GenericButtonWidget({Key? key, this.save, this.delete}) : super(key: key);
final void Function(T)? save;
final void Function(T)? delete;
@override
Widget build(BuildContext context) {
return Container(
child: Column(
children: [
InkWell(
onTap: () => save?.call(0 as T),
child: Text('Save'),
),
InkWell(
onTap: () => delete?.call(0 as T),
child: Text('Delete'),
),
],
),
);
}
}
答案3
得分: 0
你可以这样创建它:
typedef VoidCallbackDynamic = void Function(dynamic);
class GenericButtonWidget extends StatelessWidget {
const GenericButtonWidget({Key? key, this.save, this.delete})
: super(key: key);
final VoidCallback? save;
final VoidCallbackDynamic? delete;
@override
Widget build(BuildContext context) {
return Container(
child: Column(
children: [
InkWell(
onTap: save,
child: Text('Save'),
),
InkWell(
onTap: save, // 这里有错误
child: Text('Delete'),
),
],
),
);
}
}
英文:
you can create it like this:
typedef VoidCallbackDynamic = void Function(dynamic)
typedef VoidCallbackDynamic = void Function(dynamic);
class GenericButtonWidget extends StatelessWidget {
const GenericButtonWidget({Key? key, this.save, this.delete})
: super(key: key);
final VoidCallback? save;
final VoidCallbackDynamic? delete;
@override
Widget build(BuildContext context) {
return Container(
child: Column(
children: [
InkWell(
onTap: save,
child: Text('Save'),
),
InkWell(
onTap: save, //error here
child: Text('Delete'),
),
],
),
);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论