我被页面路由困住了。
这是 main.dart 中的代码
import 'package: test/routes/router.gr.dart';
import 'package:flutter/material.dart';
import 'package:test/splash_screen.dart';
import 'package:test/home_screen.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
),
debugShowCheckedModeBanner: false,
initialRoute: Router.homeScreenRoute,
onGenerateRoute: Router.onGenerateRoute,
navigatorKey: Router.navigatorKey,
);
}
}
这是自动路由器生成的 router.gr.dart 文件。
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:auto_route/auto_route.dart';
import 'package:test/home_screen.dart';
import 'package:test/viewownprofile.dart';
import 'package:test/view_other_profile.dart';
abstract class Routes {
static const homeScreenRoute = '/';
static const viewownProfile = '/viewown-profile';
static const viewotherProfile = '/viewother-profile';
static const all = {
homeScreenRoute,
viewownProfile,
viewotherProfile,
};
}
class Router extends RouterBase {
@override
Set<String> get allRoutes => Routes.all;
@Deprecated('call ExtendedNavigator.ofRouter<Router>() directly')
static ExtendedNavigatorState get navigator =>
ExtendedNavigator.ofRouter<Router>();
@override
Route<dynamic> onGenerateRoute(RouteSettings settings) {
switch (settings.name) {
case Routes.homeScreenRoute:
return MaterialPageRoute<dynamic>(
builder: (context) => HomeScreen(),
settings: settings,
);
case Routes.viewownProfile:
return MaterialPageRoute<dynamic>(
builder: (context) => ViewOwnProfile(),
settings: settings,
);
case Routes.viewotherProfile:
return MaterialPageRoute<dynamic>(
builder: (context) => ViewOtherProfile(),
settings: settings,
);
default:
return unknownRoutePage(settings.name);
}
}
}
这是 Homescreen.dart 的代码。
import 'package:flutter/material.dart';
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar (title: Text('Home')),
body: Center(
child: Card(
color: Colors.grey[300],
elevation: 10.0,
child: Container(
height: 100.0,
width: 100.0,
alignment: Alignment.center,
child: Text(
'Welcome Home!',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 20.0
),
)
),
),
)
);
}
}
我在 main.dart 文件中遇到错误。下面是错误。
The getter 'homeScreenRoute' isn't defined for the type 'Router'.
Try importing the library that defines 'homeScreenRoute', correcting the name to the name of an existing getter, or defining a getter or field named 'homeScreenRoute'.
类似的错误在生成路由上 and 导航键.