我在尝试将 flutter_bloc 集成到 flutter 项目中时遇到错误。
错误:在此 BlocBuilder
这是我正在使用的代码示例。
internet_state.dart
abstract class InternetState{}
class InitialInternetState extends InternetState{}
class LostConnectionInternetState extends InternetState{}
class GainConnectionInternetState extends InternetState{}
internet_event.dart
abstract class InternetEvent{}
class LostConnectionEvent extends InternetEvent{}
class GainConnectionEvent extends InternetEvent{}
internet_bloc.dart
class InternetBloc extends Bloc<InternetEvent, InternetState>{
final Connectivity _connectivity = Connectivity();
StreamSubscription? streamSubscription;
InternetBloc() : super(InitialInternetState()){
on<GainConnectionEvent>((event, emit) => emit(GainConnectionInternetState()));
on<LostConnectionEvent>((event, emit) => emit(LostConnectionInternetState()));
_connectivity.onConnectivityChanged.listen((event) {
if(event == ConnectivityResult.mobile || event == ConnectivityResult.wifi){
add(GainConnectionEvent());
} else {
add(LostConnectionEvent());
}
});
}
@override
Future<void> close() {
streamSubscription?.cancel();
return super.close();
}
}
main.dart
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(title),),
body: SafeArea(
child: Center(child: BlocBuilder<InternetBloc, InternetState>(
builder: (context, state) {
return const Text("hello");
},
)),
),);
}
}
我明白了。
======== 小部件库捕获异常====================================== ===================
构建 MediaQuery(MediaQueryData(size: Size(392.7, 783.3), devicePixelRatio: 2.8, textScaler: noscaling, platformBrightness: Brightness.light, padding: EdgeInsets.zero, viewPadding: EdgeInsets.zero, viewInsets: EdgeInsets 时抛出以下 ProviderNotFoundException。零,systemGestureInsets:EdgeInsets(29.8,24.0,29.8,8.0),alwaysUse24HourFormat:假,accessibleNavigation:假,highContrast:假,onOffSwitchLabels:假,disableAnimations:假,invertColors:假,boldText:假,navigationMode:传统,gestureSettings:DeviceGestureSettings (touchSlop:8.0),显示功能:[])):
错误:无法在此 BlocBuilder
发生这种情况是因为您使用了不包含提供程序的
BuildContext
你的选择。有以下几种常见场景:
您在
main.dart
中添加了一个新的提供程序并执行了热重载。
要修复此问题,请执行热重启。
您尝试阅读的提供商处于不同的路线。
提供商具有“范围”。因此,如果您在路线中插入提供者,那么 其他路线将无法访问该提供商。
您使用了
BuildContext
,它是您尝试读取的提供程序的祖先。
确保 BlocBuilder
例如,代替:
Widget build(BuildContext context) {
return Provider<Example>(
create: (_) => Example(),
// Will throw a ProviderNotFoundError, because `context` is associated
// to the widget that is the parent of `Provider<Example>`
child: Text(context.watch<Example>().toString()),
);
}
考虑使用
builder
,如下所示:
Widget build(BuildContext context) {
return Provider<Example>(
create: (_) => Example(),
// we use `builder` to obtain a new `BuildContext` that has access to the provider
builder: (context, child) {
// No longer throws
return Text(context.watch<Example>().toString());
}
);
}
如果这些解决方案都不起作用,请考虑在 StackOverflow 上寻求帮助: https://stackoverflow.com/questions/tagged/flutter
The relevant error-causing widget was:
SafeArea SafeArea:file:///Users/../Projects/flutterTest/bloc_test/lib/main.dart:37:13
When the exception was thrown, this was the stack:
#0 Provider._inheritedElementOf (package:provider/src/provider.dart:343:7)
#1 Provider.of (package:provider/src/provider.dart:293:30)
#2 ReadContext.read (package:provider/src/provider.dart:649:21)
#3 _BlocBuilderBaseState.initState (package:flutter_bloc/src/bloc_builder.dart:130:36)
#4 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:5602:55)
#5 ComponentElement.mount (package:flutter/src/widgets/framework.dart:5447:5)
... Normal element mounting (4 frames)
#9 Element.inflateWidget (package:flutter/src/widgets/framework.dart:4326:16)
#10 Element.updateChild (package:flutter/src/widgets/framework.dart:3831:20)
#11 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5496:16)
#12 Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
#13 ProxyElement.update (package:flutter/src/widgets/framework.dart:5800:5)
#14 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#15 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6743:14)
#16 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#17 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5496:16)
#18 Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
#19 StatelessElement.update (package:flutter/src/widgets/framework.dart:5547:5)
#20 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#21 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5496:16)
#22 Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
#23 StatelessElement.update (package:flutter/src/widgets/framework.dart:5547:5)
#24 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#25 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5496:16)
#26 Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
#27 StatelessElement.update (package:flutter/src/widgets/framework.dart:5547:5)
#28 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#29 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5496:16)
#30 Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
#31 ProxyElement.update (package:flutter/src/widgets/framework.dart:5800:5)
#32 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#33 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5496:16)
#34 Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
#35 ProxyElement.update (package:flutter/src/widgets/framework.dart:5800:5)
#36 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#37 Element.updateChildren (package:flutter/src/widgets/framework.dart:3964:32)
#38 MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6896:17)
#39 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#40 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5496:16)
#41 Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
#42 ProxyElement.update (package:flutter/src/widgets/framework.dart:5800:5)
#43 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#44 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5496:16)
#45 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5634:11)
#46 Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
#47 StatefulElement.update (package:flutter/src/widgets/framework.dart:5657:5)
#48 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#49 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5496:16)
#50 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5634:11)
#51 Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
#52 StatefulElement.update (package:flutter/src/widgets/framework.dart:5657:5)
#53 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#54 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5496:16)
#55 Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
#56 ProxyElement.update (package:flutter/src/widgets/framework.dart:5800:5)
#57 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#58 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5496:16)
#59 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5634:11)
#60 Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
#61 StatefulElement.update (package:flutter/src/widgets/framework.dart:5657:5)
#62 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#63 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6743:14)
#64 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#65 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5496:16)
#66 Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
#67 ProxyElement.update (package:flutter/src/widgets/framework.dart:5800:5)
#68 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#69 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6743:14)
#70 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#71 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5496:16)
#72 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5634:11)
#73 Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
#74 StatefulElement.update (package:flutter/src/widgets/framework.dart:5657:5)
#75 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#76 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5496:16)
#77 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5634:11)
#78 Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
#79 StatefulElement.update (package:flutter/src/widgets/framework.dart:5657:5)
#80 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#81 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5496:16)
#82 Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
#83 ProxyElement.update (package:flutter/src/widgets/framework.dart:5800:5)
#84 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#85 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5496:16)
#86 Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
#87 ProxyElement.update (package:flutter/src/widgets/framework.dart:5800:5)
#88 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#89 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5496:16)
#90 Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
#91 ProxyElement.update (package:flutter/src/widgets/framework.dart:5800:5)
#92 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#93 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5496:16)
#94 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5634:11)
#95 Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
#96 StatefulElement.update (package:flutter/src/widgets/framework.dart:5657:5)
#97 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#98 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5496:16)
#99 Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
#100 ProxyElement.update (package:flutter/src/widgets/framework.dart:5800:5)
#101 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#102 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5496:16)
#103 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5634:11)
#104 Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
#105 StatefulElement.update (package:flutter/src/widgets/framework.dart:5657:5)
#106 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#107 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5496:16)
#108 Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
#109 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2895:19)
#110 WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:984:21)
#111 RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:457:5)
#112 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1325:15)
#113 SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1255:9)
#114 SchedulerBinding.scheduleWarmUpFrame.<anonymous closure> (package:flutter/src/scheduler/binding.dart:978:7)
#118 _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)
(elided 3 frames from class _Timer and dart:async-patch)
====================================================================================================
我不想使用像 MultiProvider 这样的全局提供程序。
您应该在某处创建 InternetBloc 并提供它。 MultiProvider 本身并不是全局的,它只是多个。 更多信息在这里:https://bloclibrary.dev/#/flutterbloccoreconcepts?id=blocprovider
所以,如果您不想使用全局提供程序,它将看起来像这样:
class MyHomePage extends StatelessWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(title),
),
body: SafeArea(
child: Center(
child: BlocProvider<InternetBloc>(
create: ( context) => InternetBloc(),
child: BlocBuilder<InternetBloc, InternetState>(
builder: (context, state) {
return const Text("hello");
},
),
),
),
),
);
}
}
要使用 BlocBuilder,您需要在 Widget 树中放置一个 BlocProvider。最佳实践是将其放置得尽可能深。在您的情况下,只需将 BlocProvider 放在 BlocBuilder 之上。 此外 MultiProvider / MultiBlocProvider 不是全局 Provider。它只是一个提供多个 Provider/BlocProvider 的便捷小部件。
class MyHomePage extends StatelessWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(title),
),
body: SafeArea(
child: Center(
// Now everything below that can access the InternetBloc
child: BlocProvider<InternetBloc>(
create: (_) => InternetBloc(),
child: BlocBuilder<InternetBloc, InternetState>(
builder: (context, state) {
return const Text("hello");
},
),
),
),
),
);
}
}