当网络连接恢复时使用 Bloc /Cubit flutter 不会自动刷新?
When network connectivity comes back auto refresh not happen using Bloc /Cubit flutter?
我设计了 3 个底部导航按钮,即仪表板、主页、设置。由于某种网络问题,我在主屏幕上。所以导航到仪表板屏幕。该应用程序显示一条消息,例如没有互联网连接 点击重试。 点击点击重试仪表板屏幕已连接并且工作正常。然后转到主页仍然显示错误消息小部件。
当 Internet 连接返回页面时,它不刷新是否可以这样做我尝试使用下面的示例代码:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
final Connectivity connectivity=Connectivity();
return MultiBlocProvider(
providers: [
BlocProvider(
create: (context) => InternetCubit(connectivity: connectivity)),
],
child: MaterialApp(home:BottomNavScreen())
);
}
}
BottomNavScreen 构建
@override
Widget build(BuildContext context) {
Repository repository =Repository(networkService: NetworkService());
return MultiBlocProvider(
providers: [
BlocProvider(
create: (BuildContext context) => DashCubit(
repository: repository,
//todosCubit: getOtpCubit,
)),
BlocProvider(
create: (BuildContext context) => HomeCubit(
repository: repository,
//todosCubit: getOtpCubit,
)),
],
child:
BlocBuilder<InternetCubit, InternetState>(
builder: (context, state) {
return Scaffold(
body:Center(
child: PersistentTabView(
context,
controller: _controller,
screens: _buildScreens(),
items: _navBarsItems(themeData),
),
),
)
,
);
},
),
);
}
主屏幕
@override
Widget build(BuildContext context) {
return
BlocListener<InternetCubit, InternetState>(
listener: (context, state) {
if (state is InternetDisconnected ) {
Scaffold.of(context).showSnackBar(SnackBar(
content: Text('InternetDisconnected'),
backgroundColor: Colors.red,
));
}
},
child: Scaffold(
body:
BlocConsumer<HomeCubit, HomeState>(
listener: (context, state) {
},
builder: (context, state) {
if (state is ErrorState) {
return Container(
height: double.infinity,
child: ErrorTxt(
message: '${error}\n\nTap to Retry.',
ontap: _loadAlbums,
),
);
}
else if (state is HomeLoaded) {
return
Container(
);
}
},
)
),
);
}
如何解决这个问题请帮我解决这个问题
它在互联网状态连接中工作,以加载 widgets.i 尝试使用波纹管引用 link
https://github.com/TheWCKD/blocFromZeroToHero/tree/master/%237%20-%20BLoC%20通讯
BlocBuilder<InternetCubit, InternetState>(
builder: (context, state) {
if (state is InternetConnected ) {
return buildBlocConsumer();
}
else if (state is InternetDisconnected) {
return ErrorTxt(
message: 'Check Network Connection \n\n Tap to Retry ',
ontap: _loadAlbums,
);
}
return buildBlocConsumer();
是否有替代答案?
我设计了 3 个底部导航按钮,即仪表板、主页、设置。由于某种网络问题,我在主屏幕上。所以导航到仪表板屏幕。该应用程序显示一条消息,例如没有互联网连接 点击重试。 点击点击重试仪表板屏幕已连接并且工作正常。然后转到主页仍然显示错误消息小部件。
当 Internet 连接返回页面时,它不刷新是否可以这样做我尝试使用下面的示例代码:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
final Connectivity connectivity=Connectivity();
return MultiBlocProvider(
providers: [
BlocProvider(
create: (context) => InternetCubit(connectivity: connectivity)),
],
child: MaterialApp(home:BottomNavScreen())
);
} }
BottomNavScreen 构建
@override
Widget build(BuildContext context) {
Repository repository =Repository(networkService: NetworkService());
return MultiBlocProvider(
providers: [
BlocProvider(
create: (BuildContext context) => DashCubit(
repository: repository,
//todosCubit: getOtpCubit,
)),
BlocProvider(
create: (BuildContext context) => HomeCubit(
repository: repository,
//todosCubit: getOtpCubit,
)),
],
child:
BlocBuilder<InternetCubit, InternetState>(
builder: (context, state) {
return Scaffold(
body:Center(
child: PersistentTabView(
context,
controller: _controller,
screens: _buildScreens(),
items: _navBarsItems(themeData),
),
),
)
,
);
},
),
);
}
主屏幕
@override
Widget build(BuildContext context) {
return
BlocListener<InternetCubit, InternetState>(
listener: (context, state) {
if (state is InternetDisconnected ) {
Scaffold.of(context).showSnackBar(SnackBar(
content: Text('InternetDisconnected'),
backgroundColor: Colors.red,
));
}
},
child: Scaffold(
body:
BlocConsumer<HomeCubit, HomeState>(
listener: (context, state) {
},
builder: (context, state) {
if (state is ErrorState) {
return Container(
height: double.infinity,
child: ErrorTxt(
message: '${error}\n\nTap to Retry.',
ontap: _loadAlbums,
),
);
}
else if (state is HomeLoaded) {
return
Container(
);
}
},
)
),
);
}
如何解决这个问题请帮我解决这个问题
它在互联网状态连接中工作,以加载 widgets.i 尝试使用波纹管引用 link https://github.com/TheWCKD/blocFromZeroToHero/tree/master/%237%20-%20BLoC%20通讯
BlocBuilder<InternetCubit, InternetState>(
builder: (context, state) {
if (state is InternetConnected ) {
return buildBlocConsumer();
}
else if (state is InternetDisconnected) {
return ErrorTxt(
message: 'Check Network Connection \n\n Tap to Retry ',
ontap: _loadAlbums,
);
}
return buildBlocConsumer();
是否有替代答案?