首先启用dartdoc:
flutter pub global activate dartdoc
然后执行dartdoc命令
dartdoc
执行dartdoc命令后,出现如下错误:

  1. #0 _validateArgList (package:path/src/context.dart:1101:5)
  2. #1 Context.join (package:path/src/context.dart:240:5)
  3. #2 join (package:path/path.dart:262:13)
  4. #3 createDartdocOptions.<anonymous closure> (package:dartdoc/src/dartdoc_options.dart:1587:16)
  5. #4 DartdocSyntheticOption._valueAtFromSynthetic (package:dartdoc/src/dartdoc_options.dart:798:44)
  6. #5 DartdocOptionArgSynth.valueAt (package:dartdoc/src/dartdoc_options.dart:756:12)
  7. #6 DartdocOptionContext.sdkDir (package:dartdoc/src/dartdoc_options.dart:1405:44)
  8. #7 PubPackageBuilder.sdk (package:dartdoc/src/model/package_builder.dart:81:60)
  9. #8 PubPackageBuilder.buildPackageGraph (package:dartdoc/src/model/package_builder.dart:67:7)
  10. #9 Dartdoc.generateDocsBase (package:dartdoc/dartdoc.dart:175:41)
  11. #10 Dartdoc.generateDocs (package:dartdoc/dartdoc.dart:216:34)
  12. #11 _rootRun (dart:async/zone.dart:1184:13)
  13. #12 _CustomZone.run (dart:async/zone.dart:1077:19)
  14. #13 _runZoned (dart:async/zone.dart:1619:10)
  15. #14 runZonedGuarded (dart:async/zone.dart:1608:12)
  16. #15 Dartdoc.executeGuarded (package:dartdoc/dartdoc.dart:475:5)
  17. #16 main (file:///Users/chongyang/Library/flutter/.pub-cache/hosted/pub.flutter-io.cn/dartdoc-0.32.1/bin/dartdoc.dart:24:11)
  18. <asynchronous suspension>
  19. #17 _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:299:32)
  20. #18 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)

搜索到原因为未设置FLUTTER_ROOT到环境变量中。(在我的系统中设置的变量名是FLUTTER_HOME,推断dartdoc只查找默认值是FLUTTER_ROOT的环境变量)。
将FLUTTER_ROOT添加到环境变量后,文档生成成功。(如何添加环境变量,请出门左转问度娘)
生成的文档在[项目根目录]/doc/api下。
export FLUTTER_ROOT=你的flutter安装目录

环境变量配置清单

  1. export FLUTTER_ROOT=你的flutter安装目录
  2. export PUB_HOSTED_URL=https://pub.flutter-io.cn
  3. export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn
  4. export PATH=你的flutter安装目录/bin:$PATH