【问题标题】:Getting static constant to work for navigation让静态常量为导航工作
【发布时间】:2020-07-23 18:12:33
【问题描述】:

我已遵循此指南:https://medium.com/flutter-community/flutter-vi-navigation-drawer-flutter-1-0-3a05e09b0db9

特别想复制抽屉。但是,我的 Routes.dart 文件中的静态常量出现错误。

代码如下所示: 在我的 lib/routes/routes.dart 文件中:

import 'package:book_club/main.dart';

class Routes {
  static const String Profile = ProfilePage.routeName;
  static const String BookClub = BookClubPage.routeName;
  static const String Library = LibraryPage.routeName;
  static const String Search = SearchPage.routeName;
  static const String Store = StorePage.routeName;
}

它给了我以下两个错误:

  1. 错误:必须用常量值初始化常量变量。 (const_initialized_with_non_constant_value at [book_club] lib/Routes/routes.dart:2)
  2. 错误:未定义名称“ProfilePage”。 (undefined_identifier at [book_club] lib/Routes/routes.dart:2)

谁能帮助我理解我做错了什么?尝试多次阅读这篇文章,但找不到我的错误。我是一个完全的编程初学者,所以希望有一种简单明了的方法来解决这个问题:)

如果有什么不同,请使用 android studio。

【问题讨论】:

标签: flutter dart


【解决方案1】:

首先导入 ProfilePage 包, 和

static const String Profile = ProfilePage.routeName; 这里ProfilePagerouteName 也必须是静态常量,这意味着在编译之前应该知道变量的值


class AClass{
  static const yadu = '';
}

class BClass{
  static const y = AClass.yadu;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-23
    • 1970-01-01
    • 2013-05-20
    • 2022-08-14
    • 2012-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多