今日的程式碼 => GIHUB
這是一個網路判斷的一個範例,將會使用 data_connection_checker 套件,注意, GITHUB 裡面有一個檔案叫做 data_connection_checker
,他就是現在 plugin
裡面的程式碼,因為現在這個套件還沒有支援Null-Safety
,所以我是直接 clone
下來然後改程式碼。
網路判斷更多的人是使用
https://pub.dev/packages/connectivity
https://pub.dev/packages/connectivity_plus
這兩個的 Readme.md 都有非常完整的解說。
和現在的這個 [data_connection_checker 套件(https://pub.dev/packages/data_connection_checker) 一樣,都可以監聽即時的網路,如果想要修改的話,可以直接在底下 _subscription
修改程式碼。
註解:
這邊我不示範 connectivity 的原因是因為我使用他會有 bug。
flutter_bloc: ^7.2.0
data_connection_checker: ^0.3.4
cupertino_icons: ^1.0.3
abstract class NetworkEvent {}
class ListenConnection extends NetworkEvent {}
class ConnectionChanged extends NetworkEvent {
NetworkState connection;
ConnectionChanged(this.connection);
}
裡面因為有用到 StreamSubscription
,可以即時監聽資料。因此,要記得 close 掉 StreamSubscription
。
這個 StreamSubscription
可以等於 data_connection_checker 裡面提供的 DataConnectionChecker().onStatusChange
來監聽網路的 status(狀態)
是什麼。
class NetworkBloc extends Bloc<NetworkEvent, NetworkState> {
NetworkBloc() : super(ConnectionInitial());
late StreamSubscription _subscription;
@override
Stream<NetworkState> mapEventToState(NetworkEvent event) async* {
if (event is ListenConnection) {
_subscription = DataConnectionChecker().onStatusChange.listen((status) {
add(ConnectionChanged(status == DataConnectionStatus.disconnected
? ConnectionFailure()
: ConnectionSuccess()));
});
}
if (event is ConnectionChanged) yield event.connection;
}
@override
Future<void> close() {
_subscription.cancel();
return super.close();
}
}
@immutable
abstract class NetworkState {}
class ConnectionInitial extends NetworkState {}
class ConnectionSuccess extends NetworkState {}
class ConnectionFailure extends NetworkState {}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
backgroundColor: Colors.cyan[300],
body: BlocProvider(
create: (context) =>NetworkBloc()..add(ListenConnection()),
child: HomeScreen(),
),
),
);
}
}
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: BlocBuilder<NetworkBloc, NetworkState>(
builder: (context, state) {
if (state is ConnectionFailure) return Text("Failed to connect to Internet",);
if (state is ConnectionSuccess)
return Text(" Connected to Internet Successfully");
else
return SizedBox();
},
),
);
}
}