[英]Flutter BLoC BlocProvider.of() called with a context that does not contain a Bloc of type
[英]BlocProvider.of() called with a context that does not contain a Bloc of type TrackingBloc
我正在尝试向MapScreen
提供TrackingBloc
,但是当从onPressed
发送事件时,我收到错误BlocProvider.of() called with a context that does not contain a Bloc of type TrackingBloc.
MapScreen
还使用从main()
提供的MapBloc
,但对于TrackingBloc
我想让它本地化,而不是在main()
MultiBlocProvider
。 我试过:
BlocListener<TrackingBloc, TrackingState>
使用bloc:
参数,因为我被告知它只是像BlocProvider
那样提供 bloc( https://github.com/felangel/bloc/issues/930#issuecomment- 593790702 )但它没有用。MultiBlocLister
成为MultiBlocLister
的子MultiBlocProvider
并在那里设置TrackingBloc
,但仍然收到消息。MultiBlocProvider
中设置TrackingBloc
MultiBlocProvider
预期工作。 为什么 1 和 2 不向树提供TrackingBloc
? 非常感谢您的帮助。
地图屏幕:
class MapScreen extends StatefulWidget {
final String name;
final MapRepository _mapRepository;
MapScreen(
{Key key, @required this.name, @required MapRepository mapRepository})
: assert(mapRepository != null),
_mapRepository = mapRepository,
super(key: key);
@override
_MapScreenState createState() => _MapScreenState();
}
class _MapScreenState extends State<MapScreen> {
List<Marker> alerts;
LatLng userLocation;
MapController _mapController = MapController();
@override
Widget build(BuildContext context) {
return MultiBlocProvider(
providers: [
BlocProvider<TrackingBloc>(create: (context) {
return TrackingBloc();
}),
],
child: MultiBlocListener(
listeners: [
BlocListener<MapBloc, MapState>(
listener: (BuildContext context, MapState state) {
if (state is LocationStream) {
setState(() {
userLocation = (state).location;
// print(
// ' @@@@ MapBloc actual user location from stream is : $userLocation');
});
}
if (state is MapCenter) {
userLocation = (state).location;
// print(' @@@@ MapBloc initial center location is : $userLocation');
_mapController.move(userLocation, 16);
}
}),
BlocListener<TrackingBloc, TrackingState>(
// bloc: TrackingBloc(),
listener: (BuildContext context, TrackingState state) {
if (state is TrackedRoute) {
List<Position> route = (state).trackedRoute;
print(route);
}
}),
],
child: Scaffold(
主要的():
runApp(
MultiBlocProvider(
providers: [
BlocProvider<AuthenticationBloc>(
create: (context) {
return AuthenticationBloc(
userRepository: UserRepository(),
)..add(AppStarted());
},
),
BlocProvider<MapBloc>(create: (context) {
return MapBloc(
mapRepository: mapRepository,
)
..add(GetLocationStream())
..add(GetLocation());
}),
BlocProvider<TrackingBloc>(create: (context) {
return TrackingBloc();
}),
// BlocProvider<AlertBloc>(create: (context) {
// return AlertBloc(
// alertRepository: alertRepository,
// )..add(LoadAlerts());
// }),
],
child:
蝙蝠的右边,我可以看到您的代码有两处错误。
第一:您在 main 和 MapScreen 中提供多个 TrackingBloc。
第二:您在提供它的同一上下文中通过 BlocListener 访问 TrackingBloc(第二个BlocProvider(create: (context) {return TrackingBloc();})
)。 我的猜测是这就是导致错误的原因。
BlocProvider.of() 使用不包含 TrackingBloc 类型的 Bloc 的上下文调用
我认为只需在 MapScreen 中删除 BlocProvider 就可以完成这项工作。
我从小部件树中错误的位置提供了TrackingBloc
。 我可以在全局范围内提供我不需要的 bloc,因此要根据需要在本地提供它,我必须从返回MapScreen
main()
Blocbuilder
提供它。
将main()
更改为:
return MaterialApp(
home: BlocBuilder<AuthenticationBloc, AuthenticationState>(
builder: (context, state) {
if (state is Unauthenticated) {
return LoginScreen(userRepository: _userRepository);
}
if (state is Authenticated) {
// BlocProvider.of<MapBloc>(context).add(GetLocationStream());
// BlocProvider.of<AlertBloc>(context).add(LoadAlerts());
return MapScreen(
mapRepository: _mapRepository,
name: state.displayName,
// alertRepository: FirebaseAlertRepository(),
);
}
if (state is Unauthenticated) {
return LoginScreen(userRepository: _userRepository);
}
return SplashScreen();
},
),
);
到:
return MaterialApp(
home: BlocBuilder<AuthenticationBloc, AuthenticationState>(
builder: (context, state) {
if (state is Unauthenticated) {
return LoginScreen(userRepository: _userRepository);
}
if (state is Authenticated) {
// BlocProvider.of<MapBloc>(context).add(GetLocationStream());
// BlocProvider.of<AlertBloc>(context).add(LoadAlerts());
return MultiBlocProvider(
providers: [
BlocProvider<TrackingBloc>(create: (context) {
return TrackingBloc();
}),
],
child: MapScreen(
mapRepository: _mapRepository,
name: state.displayName,
// alertRepository: FirebaseAlertRepository(),
),
);
return MapScreen(
mapRepository: _mapRepository,
name: state.displayName,
// alertRepository: FirebaseAlertRepository(),
);
}
if (state is Unauthenticated) {
return LoginScreen(userRepository: _userRepository);
}
return SplashScreen();
},
),
);
使它按我的意图工作。 然后在MapScreen
我只使用不同的BlocListener
来收听全局作为MapBloc
或本地作为TrackingBloc
:
class _MapScreenState extends State<MapScreen> {
List<Marker> alerts;
LatLng userLocation;
MapController _mapController = MapController();
@override
Widget build(BuildContext context) {
return MultiBlocListener(
listeners: [
BlocListener<MapBloc, MapState>(
listener: (BuildContext context, MapState state) {
if (state is LocationStream) {
setState(() {
userLocation = (state).location;
// print(
// ' @@@@ MapBloc actual user location from stream is : $userLocation');
});
}
if (state is MapCenter) {
userLocation = (state).location;
// print(' @@@@ MapBloc initial center location is : $userLocation');
_mapController.move(userLocation, 16);
}
}),
BlocListener<TrackingBloc, TrackingState>(
// bloc: TrackingBloc(),
listener: (BuildContext context, TrackingState state) {
// userLocation = (state as LocationStream).location;
if (state is TrackedRoute) {
List<Position> route = (state).trackedRoute;
print(route);
// initialLocation = (state).location.then((value) {
// print('@@@@@@ value is : $value');
//// _mapController.move(value, 16.0);
// return value;
// }
// );
}
}),
],
child: Scaffold(
希望这会帮助其他刚开始使用flutter_bloc
,他们可能无法清楚地找到其小部件的文档使用说明。 仍然需要完全理解BlocProvider
和BlocListener
的bloc:
property 面团..干杯。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.