【问题标题】:ERROR DART: The name 'x' isn't a type so it can't be used as a type argument错误 DART:名称“x”不是类型,因此不能用作类型参数
【发布时间】:2020-04-28 14:57:06
【问题描述】:

一个类的输入部分和另一类的输出部分出错

  1. 这将是引用的主类
abstract class HttpFunction<Input, Output>
{
  Output apply(Input paramInput);
}
  1. 这将是调用前一个的类
class ComposerFunction with HttpFunction<Input, Request>
{
    RequestComposer<Input> inputConverter;

    ComposerFunction(RequestComposer inputConverter);

    ComposerFunction_(RequestComposer<Input> inputConverter)
    {
        this.inputConverter = inputConverter;
    }

    Request apply(Input input)
    {
        return this.inputConverter.compose(input);
    }
}

class InterpreterFunction with HttpFunction<Response, Output> 
{
    ResponseInterpreter<Output> responseInterpreter;

    InterpreterFunction(ResponseInterpreter outputConverter);

    InterpreterFunction_(ResponseInterpreter<Output> responseInterpreter)
    {
        this.responseInterpreter = responseInterpreter;
    }

    Output apply(Response response)
    {
        return this.responseInterpreter.interpret(response);
    }
}
  1. 这些是错误
The name 'Input' 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 'Input'.dart(non_type_as_type_argument)

The name 'Output' 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 'Output'.dart(non_type_as_type_argument)

更新

class ComposerFunction<Input> implements HttpFunction<Input, Request>
{
    ...
}

class InterpreterFunction<Output> implements HttpFunction<Response, Output>
{
   ...
}

感谢您的回答:)

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    你有:

    class ComposerFunction with HttpFunction<Input, Request>
    {
       ...
    }
    

    假设您希望 ComposerFunction 是一个可以在调用者指定的 InputRequest 类型上参数化的泛型类,您还没有将 ComposerFunction 声明为泛型类。尽管HttpFunction 一个泛型类,ComposerFunction 是一个-泛型类,它使用HttpFunction 专门用于字面上命名为Input 和@987654331 的类型@。如果您没有名为 InputRequest 的类,则会因此失败:

    名称“输入”不是类型,因此不能用作类型参数。

    要使ComposerFunction 成为泛型类,您需要向添加类型参数:

    class ComposerFunction<Input, Request> with HttpFunction<Input, Request>
    {
       ...
    }
    

    同样的事情也适用于InterpreterFunction

    此外,with 通常与mixins 一起使用。 abstract class 也允许与 Dart 2.0 及更早版本向后兼容,但由于 HttpFunction 不提供任何可重用的代码,这似乎没有用。如果你想让ComposerFunction(和InterpreterFunction)符合一个接口,那么你通常会使用implements

    【讨论】:

    • 非常感谢,这已经为我解决了这个问题,但又出现了一个问题。在第一类中,在“请求应用()”中它给了我以下错误A value of type 'Request can't be returned from method 'apply' because it has a return type of 'Request'.dart(return_of_invalid_type),在第二类中,在“输出应用()”中The argument type 'Response' can't be assigned to the parameter type 'Response'.dart(argument_type_not_assignable)
    • @jamesdlin,我有同样的错误,但没有建立一个抽象类,所以我对如何修复它有点困惑。我的声明是 class PushNotificationService {...} ,用法是 locator().unsubscribeFromTopic(appCode);我如何使它通用?
    • @jamesdin,请忽略我的评论。我错过了使用中的导入......?
    【解决方案2】:

    如果一般传递类型是您所追求的,您可以尝试如下更改您的抽象类

    abstract class HttpFunction<T, U>
    {
      U apply<T, U>(T paramInput);
    }
    

    类如下:

    class ComposerFunction with HttpFunction<Input, Request>
    {
        RequestComposer<Input> inputConverter;
    
        ComposerFunction(RequestComposer inputConverter);
    
        ComposerFunction_(RequestComposer<Input> inputConverter)
        {
            this.inputConverter = inputConverter;
        }
    
        Request apply<Input, Request>(Input input)
        {
            return this.inputConverter.compose(input);
        }
    }
    class InterpreterFunction with HttpFunction<Response, Output>
    {
        ResponseInterpreter<Output> responseInterpreter;
    
        InterpreterFunction(ResponseInterpreter outputConverter);
    
        InterpreterFunction_(ResponseInterpreter<Output> responseInterpreter)
        {
            this.responseInterpreter = responseInterpreter;
        }
    
        Output apply<Response, Output>(Response response)
        {
            return this.responseInterpreter.interpret(response);
        }
    }
    

    这是假设您创建/引用了预期类型的​​类

    【讨论】:

    • 您是否在文件中创建/导入了输入和输出类?
    • 是的,我导入了它们
    • 在 dartpad 上似乎没有任何错误。我已经宣布了其余的课程。如果可行,您能否发布其余的课程?
    猜你喜欢
    • 2021-07-14
    • 2019-04-23
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    • 2021-09-30
    • 2020-04-15
    相关资源
    最近更新 更多