Flutter路由跳转返回后Bloc状态丢失且调用BlocProvider.of()报错的原因
问题原因与解决方案
核心问题分析
- 路由跳转逻辑错误:HEHE页面的跳转按钮用
Navigator.push创建了全新的Home实例,而非通过Navigator.pop返回原首页。这会导致新的Home页面不在初始的BlocProvider上下文树中,无法获取ExampleBloc实例。 - Bloc作用域局限:原代码中
BlocProvider仅包裹了初始Home页面,当新增页面实例时,新页面脱离了Bloc的作用范围,自然找不到对应的状态管理器。
修正方案
1. 修复HEHE页面的返回逻辑
把HEHE页面里的Navigator.push替换为Navigator.pop,回到原来的首页实例:
FloatingActionButton( onPressed: () => Navigator.of(context).pop(), child: Text('back to home'), ),
2. 提升BlocProvider作用域(推荐)
如果需要在多页面共享Bloc,将BlocProvider移到MaterialApp外层,让整个应用都能访问Bloc实例,同时用create替代value来自动管理Bloc生命周期:
void main() { runApp( BlocProvider( create: (context) => ExampleBloc(), child: MaterialApp( home: Home(), ), ), ); }
3. 避免重复创建首页实例
永远不要通过push重复创建首页,返回上一页优先用pop;若需重置首页状态,可使用pushReplacement替换当前页面,但常规返回场景用pop即可。
完整修正代码
import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:equatable/equatable.dart'; void main() { runApp( BlocProvider( create: (context) => ExampleBloc(), child: MaterialApp( home: Home(), ), ), ); } class Home extends StatefulWidget { Home({Key? key}) : super(key: key); @override State<Home> createState() => _HomeState(); } class _HomeState extends State<Home> { late TextEditingController teste; late String lol; @override void initState() { teste = TextEditingController(); lol = 'ola'; super.initState(); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(), body: Column( children: <Widget>[ TextField( controller: teste, ), Center(child: BlocBuilder<ExampleBloc, ExampleState>( builder: (context, state) { return Text(state.exampleString ?? 'null'); }, )) ], ), floatingActionButton: Row( children: [ FloatingActionButton( onPressed: () { lol = teste.text; BlocProvider.of<ExampleBloc>(context) .add(ExampleUseEvent(exampleString: lol)); }, child: Icon(Icons.add), ), FloatingActionButton( onPressed: () => Navigator.push( context, MaterialPageRoute(builder: (context) => HEHE()), ), child: Text('next screen'), ), FloatingActionButton( onPressed: () => Navigator.of(context).pop(), child: Text('pop'), ), ], ), ); } @override void dispose() { teste.dispose(); super.dispose(); } } class HEHE extends StatelessWidget { const HEHE({super.key}); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(), body: const Center(child: Text('teste')), floatingActionButton: Row( children: [ FloatingActionButton( onPressed: () => Navigator.of(context).pop(), child: Text('back home')), ], ), ); } } class ExampleBloc extends Bloc<ExampleEvent, ExampleState> { ExampleBloc() : super(ExampleState()) { on<ExampleUseEvent>(exampleFuncion); } Future<void> exampleFuncion( ExampleUseEvent event, Emitter<ExampleState> emit, ) async { emit(state.copyWith( exampleString: event.exampleString, )); } } abstract class ExampleEvent {} class ExampleUseEvent extends ExampleEvent { final String exampleString; ExampleUseEvent({required this.exampleString}); } class ExampleState extends Equatable { final String? exampleString; ExampleState({ this.exampleString, }); ExampleState copyWith({ String? exampleString, }) { return ExampleState( exampleString: exampleString ?? this.exampleString, ); } @override List<Object?> get props => [ exampleString, ]; }
内容的提问来源于stack exchange,提问作者user21632175
相关产品推荐
相关产品推荐

