没有声明顶级 getter <className>

No top-level getter <className> declared

我正在扩展 flutter_gallery 示例。

我尝试创建新的画廊项目

new GalleryItem(
  title: 'Journal',
  subtitle: 'Example app coding',
  category: 'Apps',
  routeName: JournalDemo.routeName,
  buildRoute: (BuildContext context) => new JournalDemo()
),

我导入了 import '../journal/journal_all.dart';

里面我有export 'journal_demo.dart';

JournalDemoclass和ListDemo是一样的class,我只是改了class名字和状态名:

class JournalDemo extends StatefulWidget {
  JournalDemo({ Key key }) : super(key: key);

  static const String routeName = '/journal';

  @override
  JournalDemoState createState() => new JournalDemoState();
} 

class JournalDemoState extends State<JournalDemo> {
 .......

这是我得到的异常

I/flutter : ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════ I/flutter : The following NoSuchMethodError was thrown building GalleryApp(dirty; state: I/flutter : GalleryAppState(48280642)): I/flutter : No top-level getter 'JournalDemo' declared. I/flutter : NoSuchMethodError: method not found: 'JournalDemo' I/flutter : Receiver: top-level I/flutter : Arguments: [...] I/flutter : When the exception was thrown, this was the stack: I/flutter : #0 NoSuchMethodError._throwNew (dart:core-patch/errors_patch.dart:184) I/flutter : #1 kAllGalleryItems (/Users/matej/IdeaProjects/flutter/journal/lib/gallery/item.dart:51) I/flutter : #2 kAllGalleryItems (/Users/matej/IdeaProjects/flutter/journal/lib/gallery/item.dart:45)

我应该改变什么?

谢谢

您的代码中似乎缺少导入或解析错误。我会建议使用 flutter analyze 检查,正如上面的一位海报所建议的那样。

No top-level getter 'JournalDemo' declared 表示它无法在全局范围内找到值 JournalDemo。要么是没有导入,要么是这个错误之前有解析错误。