[Bug]: `Connectivity().onConnectivityChanged` does not emit if first event is `ConnectivityResult.none`
Platform
Android 14
Plugin
connectivity_plus
Version
5.0.2
Flutter SDK
3.16.7
Steps to reproduce
The problem of no stream events emitted is only occuring for the very first event and only in case it is ConnectivityResult.none.
Happy path
- Make sure you have a working internet connection
- Run the app (see below) on an Android device or emulator
- See
ConnectivityResult.wifiorConnectivityResult.mobileon the screen - Turn off the internet connection
- See
ConnectivityResult.noneon the screen
Error path
- Make sure you have no internet connection before (hot re-)starting the app
- Run the app (see below) on an Android device or emulator
- See
no stream event yet - Wait and see that there will never be an event
- Turn on the internet connection
- See correct
ConnectivityResultagain
Workaround
There is a known workaround, also outlined in this video from Randal Schwartz, where you query the first event with Connectivity().checkConnectivity() manually and prepand the stream with it.
The code could roughly look like this (taken from video linked above):
Stream<ConnectivityResult> get connectivityStream async* {
final c = Connectivity();
yield await c.checkConnectivity;
yield* c.onConnectivityChanged;
}
However, the (for me) unexpected nature of the behaviour might lead to unexpected bugs in projects of people using this API and not explicitly testing their app with no connection during app start.
Code Sample
A minimal reproducible example with README can be found here:
https://github.com/S-ecki/connectivity_bug
The main file pasted below for convenience:
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MainApp());
}
class MainApp extends StatefulWidget {
const MainApp({super.key});
@override
State<MainApp> createState() => _MainAppState();
}
class _MainAppState extends State<MainApp> {
ConnectivityResult? connectivity;
@override
void initState() {
super.initState();
Connectivity().onConnectivityChanged.listen((ConnectivityResult result) {
setState(() => connectivity = result);
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
builder: (context, child) => Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Center(
child: Text(connectivity?.toString() ?? 'no stream event yet'),
),
],
),
),
);
}
}
Screencase of problem
https://github.com/fluttercommunity/plus_plugins/assets/75510543/7809a327-7295-4ed3-b29c-48a88944a412
Flutter Doctor
[✓] Flutter (Channel stable, 3.16.7, on macOS 14.2.1 23C71 darwin-arm64, locale en-AT)
• Flutter version 3.16.7 on channel stable at /Users/secki/fvm/versions/3.16.7
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision ef1af02aea (10 days ago), 2024-01-11 15:19:26 -0600
• Engine revision 4a585b7929
• Dart version 3.2.4
• DevTools version 2.28.5
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
• Android SDK at /Users/secki/Library/Android/sdk
• Platform android-34, build-tools 34.0.0
• Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 15.0)
• Xcode at /Applications/Xcode-15.0.0.app/Contents/Developer
• Build 15A240d
• CocoaPods version 1.13.0
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 2022.3)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)
[✓] VS Code (version 1.85.2)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.80.0
[✓] Connected device (4 available)
• sdk gphone64 arm64 (mobile) • emulator-5554 • android-arm64 • Android 14 (API 34) (emulator)
• Simon’s iPhone (mobile) • 00008130-001E5D9E1A30001C • ios • iOS 17.2.1 21C66
• macOS (desktop) • macos • darwin-arm64 • macOS 14.2.1 23C71 darwin-arm64
• Chrome (web) • chrome • web-javascript • Google Chrome 120.0.6099.234
[✓] Network resources
• All expected network resources are available.
• No issues found!
Checklist before submitting a bug
- [X] I searched issues in this repository and couldn't find such bug/problem
- [X] I Google'd a solution and I couldn't find it
- [X] I searched on StackOverflow for a solution and I couldn't find it
- [X] I read the README.md file of the plugin
- [X] I'm using the latest version of the plugin
- [X] All dependencies are up to date with
flutter pub upgrade - [X] I did a
flutter clean - [X] I tried running the example project