Flutter页面导航失败,报Assertion failed(navigator.dart:4532)错误
Flutter导航断言失败问题修复
报错信息
Another exception was thrown: Assertion failed: file:///C:/src/flutter/packages/flutter/lib/src/widgets/navigator.dart:4532:12
问题代码
import 'package:flutter/material.dart'; import './interview.dart'; import './profilepage.dart'; import './googlesignup.dart'; class HomePage extends StatelessWidget { HomePage( {super.key, required this.mailId, required this.name, required this.pic}); String mailId; String name; String pic = ""; @override Widget build(context) { return Scaffold( appBar: AppBar( title: const Text("Home page"), backgroundColor: Colors.amber, actions: [ GestureDetector( onTap: () { Navigator.of(context).pop( MaterialPageRoute( builder: (context) => ProfilePage("data:imJy+r2EKkN2l65xhRjrODuDOGSOX76eNM+9T5rwYd+OOOyAR0vA6w6+gjEQiUr+bqpnJ/uGmjaRdZFh40GKjdvH1pau8C0saW81R0lAty9idmf0CrPXpzp6gr11ZILJOuAAAAAAAAAAAAAA==",mailID: mailId, name: name, img: pic)), ); }, child: CircleAvatar( radius: 30, backgroundImage: NetworkImage(pic), ), ) ], ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ const DefaultTextStyle( style: TextStyle( color: Colors.amber, fontSize: 40, fontWeight: FontWeight.bold), child: Text('Welcome to the interview'), ), const SizedBox(height: 30,), ElevatedButton( onPressed: () { Navigator.of(context).push( MaterialPageRoute( builder: (context) => Interviewimg( isL2Locked: false, isL3Locked: false, isL4Locked: false, 2), ), ); }, child: const Text("Click to Take Interview")), const SizedBox(height: 20,), Container( color: Colors.amber, child: InkWell(onTap: (){}, child:TextButton(onPressed: () { print("working");}, child: Text("TEST"),),), ) ], ), ), ); } }
问题描述
从HomePage导航到其他页面时失败,触发上述断言错误。页面内两处导航逻辑(头像点击跳转ProfilePage、按钮点击跳转Interviewimg)均失效。单独开发该页面时导航功能正常,关联其他页面后出现异常。
问题分析与修复
1. 导航方法使用错误
头像点击事件中误用Navigator.pop()来跳转页面——pop()的作用是关闭当前页面返回上一级,而非打开新页面,这会导致导航逻辑混乱触发断言错误。
修复代码:
将Navigator.of(context).pop()改为Navigator.of(context).push():
GestureDetector( onTap: () { Navigator.of(context).push( MaterialPageRoute( builder: (context) => ProfilePage( "data:imJy+r2EKkN2l65xhRjrODuDOGSOX76eNM+9T5rwYd+OOOyAR0vA6w6+gjEQiUr+bqpnJ/uGmjaRdZFh40GKjdvH1pau8C0saW81R0lAty9idmf0CrPXpzp6gr11ZILJOuAAAAAAAAAAAAAA==", mailID: mailId, name: name, img: pic ), ), ); }, child: CircleAvatar( radius: 30, backgroundImage: NetworkImage(pic), ), )
2. 页面构造函数传参错误
跳转Interviewimg时,最后一个参数2未指定参数名,不符合Dart构造函数传参规范,会导致初始化失败。
修复代码:
根据Interviewimg的构造函数定义,为参数添加正确的名称(假设参数名为level,需根据实际代码调整):
ElevatedButton( onPressed: () { Navigator.of(context).push( MaterialPageRoute( builder: (context) => Interviewimg( isL2Locked: false, isL3Locked: false, isL4Locked: false, level: 2, // 添加参数名 ), ), ); }, child: const Text("Click to Take Interview"), )
3. 额外优化(非导航错误,但符合规范)
StatelessWidget的成员变量建议用final修饰,避免不必要的初始化赋值:
class HomePage extends StatelessWidget { final String mailId; final String name; final String pic; HomePage({super.key, required this.mailId, required this.name, required this.pic}); // ... 其他代码不变 }
总结
修复上述两处核心错误后,导航功能即可恢复正常:
- 用
Navigator.push()替代错误的pop()实现页面跳转 - 为
Interviewimg构造函数参数补充正确的参数名
内容的提问来源于stack exchange,提问作者suryaprakashrp04
相关产品推荐
相关产品推荐

