riverpod
riverpod copied to clipboard
type `Null` is not a subtype of type `_StateReader` in type cast
Describe the bug Occasionally, my Flutter application crashes with a Null type cast error related to Riverpod's ProviderContainer. The crash is infrequent ( but has been consistently logged in Crashlytics.
Here is the stack trace:
Fatal Exception: io.flutter.plugins.firebase.crashlytics.FlutterError: type 'Null' is not a subtype of type '_StateReader' in type cast. Error thrown building Widget-[<5085>].
#00 pc 0x647def com.example.app (MapBase.entries.<anonymous closure> [maps.dart:76])
#01 pc 0x6518bf com.example.app (new ProviderContainer [iterable.dart:403])
#02 pc 0x6515ab com.example.app (ProviderScopeState.initState [framework.dart:179])
#03 pc 0x7c950b com.example.app (StatefulElement._firstBuild [framework.dart:5602])
#04 pc 0x3fd717 com.example.app (ComponentElement.mount [framework.dart:5447])
#05 pc 0x98308b com.example.app (Element.inflateWidget [framework.dart:4326])
To Reproduce
The error is intermittent and does not occur regularly, making it challenging to provide a precise reproduction path.
This code snippet is where the error is suspected to originate. I have no idea how a null value could occur within the _stateReaders
map.
This is how I run my app in main.dart
:
runApp(ProviderScope(
overrides: [remoteConfigManagerProvider.overrideWithValue(remoteConfigManager)],
child: const App(),
));
Expected behavior
It should never let a null value be added to the _stateReaders
or handle it gracefully.
Do you maybe have a more complete example possible?
There's no way the code you pointed to could trigger a null error. There's no cast nor !
used, and entry.value
is non-nullable.
It's possible that there's something deeper going on, such as maybe a Dart bug. But I'd need to have a better example to reproduce. As is, I'm unlikely to be able to do anything
Thank you for the quick response!
Yes, there is definitely something very weird going on here. It is unfortunately such a seldomly occurring issue that I don't have more to go on than the crashlytics issue. But I will try to find more relevant code:
The chat screen where it happens most also has another ProviderScope
(We use flutter_chat_ui
in case that is relevant).
statusWidgetBuilder: (Color color, types.Message m) => ProviderScope(
overrides: [
colorValueProvider.overrideWithValue(color.value),
messageProvider.overrideWithValue(m),
],
child: const ChatStatus(),
),
I could also give you access to the app if required but not sure how that would help since I've never seen the error occur myself, only on crashlytics.
On another note, regarding the possibility of the null error being triggered, the problematic cast is happening in the .entries
(code from MapBase
):
Iterable<MapEntry<K, V>> get entries {
return keys.map((K key) => MapEntry<K, V>(key, this[key] as V));
}
After this, entry.value
is definitely non-nullable. However, before this cast, it could theoretically be null. But I have no idea how something nullable could get into the map or how a key could remain with the value (this[key]
) somehow gone. After all, any .remove
should also remove the key... What do you think?
My immediate guess would be that this is another putIfAbsent related bug.
Dart had one before were we could end-up assigning null to a non-nullable value, which broke soo many thing. Could be a variant of it
This is not something I can act on due to lack of information.
Since I cannot reproduce the issue, and this issue doesn't seem to get much activity, I'll close this. Feel free to reopen a new issue if you still have the problem, but I'll need a reproduction example
This is actually an issue. @rrousselGit I'm having the same issue in an App. It's happening in places that use scope overrides.
Another exception was thrown: type 'Null' is not a subtype of type '_StateReader' in type cast
Another exception was thrown: type 'Null' is not a subtype of type '_StateReader' in type cast
Another exception was thrown: type 'Null' is not a subtype of type '_StateReader' in type cast
Another exception was thrown: type 'Null' is not a subtype of type '_StateReader' in type cast
Another exception was thrown: type 'Null' is not a subtype of type '_StateReader' in type cast
Another exception was thrown: type 'Null' is not a subtype of type '_StateReader' in type cast
Another exception was thrown: type 'Null' is not a subtype of type '_StateReader' in type cast
Another exception was thrown: type 'Null' is not a subtype of type '_StateReader' in type cast
Another exception was thrown: type 'Null' is not a subtype of type '_StateReader' in type cast
Please open a new issue, with a way to reproduce the problem.