如何在Flutter中创建一个通用的回调函数?

huangapple go评论52阅读模式
英文:

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&lt;T&gt;(T)? save;  
final Function&lt;T&gt;(T)? delete;

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: [
          InkWell(
            onTap: () =&gt; save,
            child: Text(&#39;Save&#39;),
          ),
            InkWell(
            onTap: save, //error here
            child: Text(&#39;Delete&#39;),
          ),
        ],
      ),
    );
  }
}
}

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&lt;T&gt;(T a) save = &lt;T&gt;(T a) {
  print(a);
  return a;
};


void main() {
  save(5);
}

but this does not, because of the same error you mentioned.

Function&lt;T&gt;(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&lt;T&gt; 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: () =&gt; save?.call(0 as T),
            child: Text(&#39;Save&#39;),
          ),
          InkWell(
            onTap: () =&gt; delete?.call(0 as T),
            child: Text(&#39;Delete&#39;),
          ),
        ],
      ),
    );
  }
}

这是你提供的代码的翻译部分。

英文:

You can use call function. Like:

class GenericButtonWidget&lt;T&gt; 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: () =&gt; save?.call(0 as T),
            child: Text(&#39;Save&#39;),
          ),
          InkWell(
            onTap: () =&gt; delete?.call(0 as T),
            child: Text(&#39;Delete&#39;),
          ),
        ],
      ),
    );
  }
}

答案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(&#39;Save&#39;),
          ),
          InkWell(
            onTap: save, //error here
            child: Text(&#39;Delete&#39;),
          ),
        ],
      ),
    );
  }
}

huangapple
  • 本文由 发表于 2023年3月8日 18:41:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75671984.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定