بنقرة واحدة
add-check-internet
Add internet connectivity checking before executing a Cubit method with optional retry
التثبيت باستخدام Codex أو Claude انسخ هذا Prompt والصقه في Codex أو Claude أو مساعد آخر ليراجع صفحة Skill ويثبّتها لك.
القائمة
Add internet connectivity checking before executing a Cubit method with optional retry
التثبيت باستخدام Codex أو Claude انسخ هذا Prompt والصقه في Codex أو Claude أو مساعد آخر ليراجع صفحة Skill ويثبّتها لك.
استنادا إلى تصنيف SOC المهني
Add custom error handling with catchError to a mix() call for logging, error conversion, or suppression
Add debounce to delay method execution until after a period of inactivity for search or validation
Add EffectQueue to state for triggering multiple sequential one-time UI effects
Add Effect fields to state class for one-time UI notifications like snackbars, navigation, or form clearing
Add error state handling to widgets using context.isFailed() and context.getException()
Add freshness caching to prevent redundant method executions within a time period
| name | add-check-internet |
| description | Add internet connectivity checking before executing a Cubit method with optional retry |
This skill adds internet connectivity checking before executing a Cubit method.
Adds the checkInternet parameter to a mix() call to:
Ask the user which Cubit method needs internet checking, or identify methods that:
Add checkInternet: checkInternet to the mix() call:
import 'package:bloc_superpowers/bloc_superpowers.dart';
class UserCubit extends Cubit<User> {
UserCubit() : super(User());
void loadData() => mix(
key: this,
checkInternet: checkInternet, // Add this line
() async {
final user = await api.loadUser();
emit(user);
},
);
}
When internet is unavailable with default settings:
context.isFailed() returns true)ConnectionException is thrown with message "No internet connection"UserExceptionDialog shows the error (if configured)Note: This only checks device connectivity, not whether your specific server is reachable.
checkInternet(
abortSilently: false, // If true, don't throw exception
ifOpenDialog: true, // If true, show error dialog
maxRetryDelay: 1.sec, // Recheck interval when combined with retry
)
For background operations that should quietly skip when offline:
void syncInBackground() => mix(
key: this,
checkInternet: checkInternet(abortSilently: true),
() async {
await api.syncData();
},
);
The method returns immediately without executing or throwing. No error dialog appears.
To handle the error in your widget instead of showing a dialog:
void loadData() => mix(
key: this,
checkInternet: checkInternet(ifOpenDialog: false),
() async {
final data = await api.getData();
emit(data);
},
);
Then in the widget:
Widget build(BuildContext context) {
if (context.isFailed(UserCubit)) {
return Column(
children: [
const Text('No internet connection'),
ElevatedButton(
onPressed: () => context.read<UserCubit>().loadData(),
child: const Text('Retry'),
),
],
);
}
// ... rest of widget
}
For critical data that must load eventually:
void loadInitialData() => mix(
key: this,
checkInternet: checkInternet,
retry: retry.unlimited,
() async {
final data = await api.getInitialData();
emit(data);
},
);
Internet connectivity is rechecked before each retry attempt.
Control how often to recheck connectivity:
void loadData() => mix(
key: this,
checkInternet: checkInternet(maxRetryDelay: 500.millis),
retry: retry.unlimited(maxDelay: 5.sec),
() async {
final data = await api.getData();
emit(data);
},
);
void fetchProducts() => mix(
key: this,
checkInternet: checkInternet,
() async {
final products = await api.getProducts();
emit(state.copyWith(products: products));
},
);
void syncData() => mix(
key: this,
checkInternet: checkInternet(abortSilently: true),
() async {
await api.sync();
},
);
void loadAppConfig() => mix(
key: this,
checkInternet: checkInternet(maxRetryDelay: 1.sec),
retry: retry.unlimited,
() async {
final config = await api.getConfig();
emit(config);
},
);
void loadData() => mix(
key: this,
checkInternet: checkInternet,
retry: retry(maxRetries: 3),
catchError: (error, stackTrace) {
if (error is ConnectionException) {
throw UserException('Please check your internet connection');
}
throw UserException('Failed to load data. Please try again.');
},
() async {
final data = await api.getData();
emit(data);
},
);
| Configuration | On No Internet |
|---|---|
checkInternet (default) | Throws ConnectionException, shows dialog |
checkInternet(abortSilently: true) | Returns silently, no error |
checkInternet(ifOpenDialog: false) | Throws ConnectionException, no dialog |
checkInternet + retry.unlimited | Keeps retrying until internet returns |
Ask the user: