Dart:通用方法不起作用

时间:2017-02-02 14:49:00

标签: generics dart dart-pub

我遇到泛型方法的问题。 举个例子,我用Angular2在WebStorm中生成了一个新项目。 我使用dart 1.22.0-dev 10.1,但我也遇到了dart 1.21.1的问题。 我在app_component.dart文件中添加了一个类:

import 'package:angular2/core.dart';
import 'package:angular2_components/angular2_components.dart';

import 'package:deleteMe/hello_dialog/hello_dialog.dart';

@Component(
  selector: 'my-app',
  styleUrls: const ['app_component.css'],
  templateUrl: 'app_component.html',
  directives: const [materialDirectives, HelloDialog],
  providers: const [materialProviders],
)
class AppComponent {
  // Nothing here yet. All logic is in HelloDialog.
}


class abc {

  T foo<T>(T value) {
    return value;
  }
}

分析器不会抛出任何错误,但是,当我尝试运行该示例时,我从pub中收到以下错误:

[DirectiveProcessor]:
  Failed with 4 errors
Error 1: line 23, column 5 of lib\app_component.dart and parts: Expected to find ';'
  T foo<T>(T value) {
    ^^^
Error 2: line 23, column 8 of lib\app_component.dart and parts: Expected a class member
  T foo<T>(T value) {
       ^
Error 3: line 23, column 8 of lib\app_component.dart and parts: Unexpected token '<'
  T foo<T>(T value) {
       ^
Error 4: line 23, column 10 of lib\app_component.dart and parts: Operator declarations must be preceeded by the keyword 'operator'
  T foo<T>(T value) {
         ^
Build error:
Transform DirectiveProcessor on deleteMe|lib/app_component.dart threw error: Error in lib/app_component.dart and parts: Expected to find ';'

Error in lib/app_component.dart and parts: Expected a class member

Error in lib/app_component.dart and parts: Unexpected token '<'

Error in lib/app_component.dart and parts: Operator declarations must be preceeded by the keyword 'operator'

package:analyzer/analyzer.dart 116                                   _parseSource
package:analyzer/analyzer.dart 44                                    parseCompilationUnit
package:angular2/src/transform/directive_processor/rewriter.dart 31  createNgMeta.<async>
dart:async-patch/async_patch.dart 27                                 _asyncThenWrapperHelper.<fn>
dart:async/zone.dart 1158                                            _rootRunUnary
dart:async/zone.dart 1037                                            _CustomZone.runUnary
dart:async/future_impl.dart 131                                      _FutureListener.handleValue
dart:async/future_impl.dart 637                                      _Future._propagateToListeners.handleValueCallback
dart:async/future_impl.dart 667                                      _Future._propagateToListeners
dart:async/future_impl.dart 467                                      _Future._complete
dart:async/future_impl.dart 52                                       _SyncCompleter.complete
package:angular2/src/transform/common/logging.dart 27                logElapsedAsync.<async>
dart:async-patch/async_patch.dart 27                                 _asyncThenWrapperHelper.<fn>
dart:async/zone.dart 1158                                            _rootRunUnary
dart:async/zone.dart 1037                                            _CustomZone.runUnary
dart:async/future_impl.dart 131                                      _FutureListener.handleValue
dart:async/future_impl.dart 637                                      _Future._propagateToListeners.handleValueCallback
dart:async/future_impl.dart 667                                      _Future._propagateToListeners
dart:async/future_impl.dart 477                                      _Future._completeWithValue
dart:async/future_impl.dart 528                                      _Future._asyncComplete.<fn>
dart:async/zone.dart 1150                                            _rootRun
dart:async/zone.dart 1026                                            _CustomZone.run
dart:async/zone.dart 953                                             _CustomZone.bindCallback.<fn>
dart:async/schedule_microtask.dart 41                                _microtaskLoop
dart:async/schedule_microtask.dart 50                                _startMicrotaskLoop
dart:isolate-patch/isolate_patch.dart 96                             _runPendingImmediateCallback
dart:isolate-patch/isolate_patch.dart 149                            _RawReceivePortImpl._handleMessage

package:analyzer/analyzer.dart 116                                   _parseSource
package:analyzer/analyzer.dart 44                                    parseCompilationUnit
package:angular2/src/transform/directive_processor/rewriter.dart 31  createNgMeta.<async>
dart:async                                                           _SyncCompleter.complete
package:angular2/src/transform/common/logging.dart 27                logElapsedAsync.<async>

1 个答案:

答案 0 :(得分:3)

Angular2尚不支持通用方法 - 我们依赖于没有该支持的分析器版本。在3.0.0分支的未来版本中查找它。