flutter 返回“Null”的go-router不是String的子类型

ryevplcw  于 2023-04-13  发布在  Flutter
关注(0)|答案(1)|浏览(123)

我正在尝试在我的路由上go-router。第一次它工作,但一旦我刷新它工作。

final GoRouter _router = GoRouter(
  debugLogDiagnostics: true,
  initialLocation: '/',
  routes: [
    GoRoute(
      path: '/',
      builder: (BuildContext context, GoRouterState state) {
        return const MainPage();
      },
    ),
    GoRoute(
      path: '/product/:id',
      name: 'product',
      builder: (context, state){
        final id = state.params['id']!;
        return DetailsView(id:id);
        },
    ),
    GoRoute(
      path: '/homepage',
      builder: (BuildContext context, GoRouterState state) {
        return const HomePage();
      },
    ),
  ],
);

即使传递任何值也不起作用

onTap: () {
        context.pushNamed("product", params: {'id':"dsadklaskld"});
   
      },

下面是我导航到的有状态小部件。detailsView.dart

class DetailsView extends StatefulWidget {
  final String id;

  /// Constructs a [DetailsScreen]
  const DetailsView({super.key, required this.id});
  @override
  State<DetailsView> createState() => _DetailsViewState();
}
class _DetailsViewState extends State<DetailsView> {
.... 
  late String id = widget.id;
}
2ledvvac

2ledvvac1#

id的值为null,所以我不得不把它放在initializedata()函数中。

相关问题