-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[firebase_remote_config]: onConfigUpdated doesn't support multiple listeners #13495
Open
1 task done
Labels
Needs Attention
This issue needs maintainer attention.
platform: android
Issues / PRs which are specifically for Android.
plugin: remote_config
type: bug
Something isn't working
Comments
Colman
added
Needs Attention
This issue needs maintainer attention.
type: bug
Something isn't working
labels
Oct 11, 2024
SelaseKay
added
plugin: remote_config
platform: android
Issues / PRs which are specifically for Android.
labels
Oct 14, 2024
Hi @Colman , thanks for the report. I'm able to reproduce this issue. Steps to Reproduce
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
StreamSubscription? subscription;
RemoteConfigUpdate? update;
late StreamSubscription<RemoteConfigUpdate> _subscription1;
late StreamSubscription<RemoteConfigUpdate> _subscription2;
@override
void initState() {
super.initState();
_subscription1 = FirebaseRemoteConfig.instance.onConfigUpdated.listen(
(event) {
print('event1');
},
);
_subscription2 = FirebaseRemoteConfig.instance.onConfigUpdated.listen(
(event) {
print('event2');
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Remote Config Example'),
),
body: Column(
children: [
_ButtonAndText(
defaultText: 'Not initialized',
buttonText: 'Initialize',
onPressed: () async {
final FirebaseRemoteConfig remoteConfig =
FirebaseRemoteConfig.instance;
await remoteConfig.setConfigSettings(
RemoteConfigSettings(
fetchTimeout: const Duration(seconds: 10),
minimumFetchInterval: const Duration(hours: 1),
),
);
await remoteConfig.setDefaults(<String, dynamic>{
'welcome': 'default welcome',
'hello': 'default hello',
});
RemoteConfigValue(null, ValueSource.valueStatic);
return 'Initialized';
},
),
_ButtonAndText(
defaultText: 'No data',
onPressed: () async {
try {
final FirebaseRemoteConfig remoteConfig =
FirebaseRemoteConfig.instance;
// Using zero duration to force fetching from remote server.
await remoteConfig.setConfigSettings(
RemoteConfigSettings(
fetchTimeout: const Duration(seconds: 10),
minimumFetchInterval: Duration.zero,
),
);
await remoteConfig.fetchAndActivate();
return 'Fetched: ${remoteConfig.getString('welcome')}';
} on PlatformException catch (exception) {
// Fetch exception.
print(exception);
return 'Exception: $exception';
} catch (exception) {
print(exception);
return 'Unable to fetch remote config. Cached or default values will be '
'used';
}
},
buttonText: 'Fetch Activate',
),
_ButtonAndText(
defaultText: update != null
? 'Updated keys: ${update?.updatedKeys}'
: 'No data',
onPressed: () async {
try {
final FirebaseRemoteConfig remoteConfig =
FirebaseRemoteConfig.instance;
if (subscription != null) {
await subscription!.cancel();
setState(() {
subscription = null;
});
return 'Listening cancelled';
}
setState(() {
subscription =
remoteConfig.onConfigUpdated.listen((event) async {
// Make new values available to the app.
debugPrint('Remote config updated');
await remoteConfig.activate();
setState(() {
update = event;
});
});
});
return 'Listening, waiting for update...';
} on PlatformException catch (exception) {
// Fetch exception.
print(exception);
return 'Exception: $exception';
} catch (exception) {
print(exception);
return 'Unable to listen to remote config. Cached or default values will be '
'used';
}
},
buttonText: subscription != null ? 'Cancel' : 'Listen',
),
],
),
);
}
@override
void dispose() {
_subscription1.cancel();
_subscription2.cancel();
super.dispose();
}
}
class _ButtonAndText extends StatefulWidget {
const _ButtonAndText({
Key? key,
required this.defaultText,
required this.onPressed,
required this.buttonText,
}) : super(key: key);
final String defaultText;
final String buttonText;
final Future<String> Function() onPressed;
@override
State<_ButtonAndText> createState() => _ButtonAndTextState();
}
class _ButtonAndTextState extends State<_ButtonAndText> {
String? _text;
// Update text when widget is updated.
@override
void didUpdateWidget(covariant _ButtonAndText oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.defaultText != oldWidget.defaultText) {
setState(() {
_text = widget.defaultText;
});
}
}
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(8),
child: Row(
children: [
Text(_text ?? widget.defaultText),
const Spacer(),
ElevatedButton(
onPressed: () async {
final result = await widget.onPressed();
setState(() {
_text = result;
});
},
child: Text(widget.buttonText),
),
],
),
);
}
} |
Hi @Colman , as a workaround, you can do something like: final _onConfigUpdatedStream = FirebaseRemoteConfig.instance.onConfigUpdated;
@override
void initState() {
super.initState();
_onConfigUpdatedStream.listen(
(event) {
print('event1');
},
);
_onConfigUpdatedStream.listen(
(event) {
print('event2');
},
);
} |
10 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
Needs Attention
This issue needs maintainer attention.
platform: android
Issues / PRs which are specifically for Android.
plugin: remote_config
type: bug
Something isn't working
Is there an existing issue for this?
Which plugins are affected?
Remote Config
Which platforms are affected?
Android
Description
When adding multiple listeners to the onConfigUpdated stream, only the most recently added listener fires when there's an event
Reproducing the issue
The following will print "event2" but not "event1" when the config is updated:
Firebase Core version
3.4.0
Flutter Version
3.22.2
Relevant Log Output
No response
Flutter dependencies
Expand
Flutter dependencies
snippetAdditional context and comments
No response
The text was updated successfully, but these errors were encountered: