You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter定时导航报错:返回类型'void'不符合Widget要求排查

解决Flutter导航报错:返回类型'void'并非闭包上下文所要求的'Widget'类型

错误原因

你在Navigator.push的MaterialPageRoute构建器里调用了main()函数,但main()是返回void的应用入口函数,而builder闭包要求必须返回一个Widget类型的页面组件,这直接导致了类型不匹配的报错。

修复步骤

  1. 明确目标页面组件:确认你要跳转的目标页面(比如假设你要跳转到HomeScreen,这个组件应该在你导入的main.dart中)
  2. 替换错误的构建器内容:把builder: (context)=>main()改成builder: (context)=> 你的目标页面(),比如HomeScreen()
  3. 可选优化:在initState中使用context前,最好加上mounted判断,避免组件已销毁时触发导航导致异常

修改后的核心代码片段

@override
void initState() {
  super.initState();
  Timer(Duration(seconds: 5), () {
    // 先判断组件是否仍挂载,避免异常
    if(mounted){
      // 替换成你的目标页面组件,比如HomeScreen()
      Navigator.of(context).push(MaterialPageRoute(builder: (context)=> HomeScreen()));
    }
  });
}

完整修正后的代码(假设目标页面是HomeScreen)

import 'package:flutter/material.dart';
import 'dart:async';
import 'main.dart'; // 确保这里导入了目标页面组件

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: "Flutter App",
      theme: ThemeData(
        appBarTheme: AppBarTheme(
          color: Colors.deepOrangeAccent,
        ),
        primarySwatch: Colors.indigo,
      ),
      home: LoginScreen(),
    );
  }
}

class LoginScreen extends StatefulWidget {
  const LoginScreen({Key? key}) : super(key: key);

  @override
  State<LoginScreen> createState() => _LoginScreenState();
}

class _LoginScreenState extends State<LoginScreen> {

  @override
  void initState() {
    super.initState();
    Timer(Duration(seconds: 5), () {
      if(mounted){
        // 替换为实际的目标页面组件
        Navigator.of(context).push(MaterialPageRoute(builder: (context)=> HomeScreen()));
      }
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.indigo,
      body: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 20.0),
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Center(
                child: Image.asset(
                  "assets/images/logo.png",
                  height: 200,
                  width: 200,
                ),
              ),
              SizedBox(
                height: 20,
              ),
              Text(
                "Fresh Food Restaurant",
                style: TextStyle(
                  color: Colors.deepOrangeAccent,
                  fontSize: 25,
                  fontWeight: FontWeight.w900,
                ),
              ),
              SizedBox(
                height: 150,
              ),
              SizedBox(
                height: 100,
              ),
              FloatingActionButton(
                backgroundColor: const Color(0xff03dac6),
                foregroundColor: Colors.black,
                onPressed: () {},
                child: Icon(Icons.arrow_forward),
              )
            ],
          ),
        ),
      ),
    );
  }
}

内容的提问来源于stack exchange,提问作者Muhammad Imran

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 18:35:10