Skip to content

Commit

Permalink
docs: added instructions to pause network request on app background (#60
Browse files Browse the repository at this point in the history
)
  • Loading branch information
OutdatedGuy authored Sep 16, 2024
1 parent 93071f9 commit 071dcf6
Showing 1 changed file with 48 additions and 2 deletions.
50 changes: 48 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,12 @@ final connection = InternetConnection.createInstance(
);
```

> **Note**
> [!IMPORTANT]
>
> Make sure the custom `Uri`s have no caching enabled. Otherwise, the results
> may be inaccurate.

> **Note**
> [!IMPORTANT]
>
> On `web` platform, make sure the custom `Uri`s are not CORS blocked.
> Otherwise, the results may be inaccurate.
Expand Down Expand Up @@ -153,6 +153,51 @@ final connection = InternetConnection.createInstance(
);
```

### 7. Pause and Resume on App Lifecycle Changes

For situation where you want to pause any network requests when the app goes
into the background and resume them when the app comes back into the foreground
(see [issue #27]):

```dart
class MyWidget extends StatefulWidget {
const MyWidget({super.key});
@override
State<MyWidget> createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
late final StreamSubscription<InternetStatus> _subscription;
late final AppLifecycleListener _listener;
@override
void initState() {
super.initState();
_subscription = InternetConnection().onStatusChange.listen((status) {
// Handle internet status changes
});
_listener = AppLifecycleListener(
onResume: _subscription.resume,
onHide: _subscription.pause,
onPause: _subscription.pause,
);
}
@override
void dispose() {
_subscription.cancel();
_listener.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
// Build your widget
}
}
```

#### Default `Uri`s

The `InternetConnection` class uses the following `Uri`s by default:
Expand Down Expand Up @@ -203,3 +248,4 @@ supported by the [internet_connection_checker] package.
[issues_closed]: https://github.com/OutdatedGuy/internet_connection_checker_plus/issues?q=is%3Aissue+is%3Aclosed
[internet_connection_checker]: https://github.com/RounakTadvi/internet_connection_checker
[data_connection_checker]: https://pub.dev/packages/data_connection_checker
[issue #27]: https://github.com/OutdatedGuy/internet_connection_checker_plus/issues/27

0 comments on commit 071dcf6

Please sign in to comment.