如何为全局密钥错误设置多重用法

时间:2019-06-29 14:42:07

标签: flutter dart

我正在尝试如下设置一组命名路由。

void main() => runApp(FlashChat());

class FlashChat extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark().copyWith(
        textTheme: TextTheme(
          body1: TextStyle(color: Colors.black54),
        ),
      ),
      initialRoute: WelcomeScreen.id,
      routes: {
        WelcomeScreen.id : (context) => WelcomeScreen(),
        LoginScreen.id : (context) => LoginScreen(),
        RegistrationScreen.id : (context) => RegistrationScreen(),
        ChatScreen.id : (context) => ChatScreen(),
      },
    );
  }
}

这是如何为每条路线设置id值的示例。

class WelcomeScreen extends StatefulWidget {

  static String id = '/welcome';

  @override
  _WelcomeScreenState createState() => _WelcomeScreenState();
}

这将引发以下错误。

Launching lib/main.dart on iPhone X in debug mode...
Running Xcode build...
Xcode build done.                                            9.2s
Syncing files to device iPhone X...
flutter: ══╡ EXCEPTION CAUGHT BY FLUTTER FRAMEWORK ╞═════════════════════════════════════════════════════════
flutter: The following message was thrown:
flutter: Could not navigate to initial route.
flutter: The requested route name was: "/welcome"
flutter: The following routes were therefore attempted:
flutter:  * /
flutter:  * /welcome
flutter: This resulted in the following objects:
flutter:  * null
flutter:  * MaterialPageRoute<dynamic>(RouteSettings("/welcome", null), animation: null)
flutter: One or more of those objects was null, and therefore the initial route specified will be ignored and
flutter: "/" will be used instead.
flutter: ════════════════════════════════════════════════════════════════════════════════════════════════════
flutter: Another exception was thrown: Could not find a generator for route RouteSettings("/", null) in the _WidgetsAppState.
flutter: Another exception was thrown: A GlobalKey was used multiple times inside one widget's child list.
flutter: Another exception was thrown: A GlobalKey was used multiple times inside one widget's child list.

如果我将路由设置为“ /”而不是“ / welcome”,则可以正常工作。我不明白错误要说什么。如何重复使用全局密钥?如何解决此错误?请指教。谢谢。

编辑

通过将字符串值从“ / welcome”更改为“ welcome”(删除了/)来解决此问题,但仍不确定其引起问题的原因。

0 个答案:

没有答案