@@ -21,18 +21,69 @@ class StreamChatConfiguration extends InheritedWidget {
2121 bool updateShouldNotify (StreamChatConfiguration oldWidget) =>
2222 data != oldWidget.data;
2323
24- /// Use this method to get the current [StreamChatThemeData] instance
24+ /// Finds the [StreamChatConfigurationData] from the closest
25+ /// [StreamChatConfiguration] ancestor that encloses the given context.
26+ ///
27+ /// This will throw a [FlutterError] if no [StreamChatConfiguration] is found
28+ /// in the widget tree above the given context.
29+ ///
30+ /// Typical usage:
31+ ///
32+ /// ```dart
33+ /// final config = StreamChatConfiguration.of(context);
34+ /// ```
35+ ///
36+ /// If you're calling this in the same `build()` method that creates the
37+ /// `StreamChatConfiguration` , consider using a `Builder` or refactoring into
38+ /// a separate widget to obtain a context below the [StreamChatConfiguration] .
39+ ///
40+ /// If you want to return null instead of throwing, use [maybeOf] .
2541 static StreamChatConfigurationData of (BuildContext context) {
26- final streamChatConfiguration =
27- context. dependOnInheritedWidgetOfExactType < StreamChatConfiguration >() ;
42+ final result = maybeOf (context);
43+ if (result != null ) return result ;
2844
29- assert (
30- streamChatConfiguration != null ,
31- '''
32- You must have a StreamChatConfigurationProvider widget at the top of your widget tree''' ,
33- );
45+ throw FlutterError .fromParts (< DiagnosticsNode > [
46+ ErrorSummary (
47+ 'StreamChatConfiguration.of() called with a context that does not '
48+ 'contain a StreamChatConfiguration.' ,
49+ ),
50+ ErrorDescription (
51+ 'No StreamChatConfiguration ancestor could be found starting from the '
52+ 'context that was passed to StreamChatConfiguration.of(). This usually '
53+ 'happens when the context used comes from the widget that creates the '
54+ 'StreamChatConfiguration itself.' ,
55+ ),
56+ ErrorHint (
57+ 'To fix this, ensure that you are using a context that is a descendant '
58+ 'of the StreamChatConfiguration. You can use a Builder to get a new '
59+ 'context that is under the StreamChatConfiguration:\n\n '
60+ ' Builder(\n '
61+ ' builder: (context) {\n '
62+ ' final config = StreamChatConfiguration.of(context);\n '
63+ ' ...\n '
64+ ' },\n '
65+ ' )' ,
66+ ),
67+ ErrorHint (
68+ 'Alternatively, split your build method into smaller widgets so that '
69+ 'you get a new BuildContext that is below the StreamChatConfiguration '
70+ 'in the widget tree.' ,
71+ ),
72+ context.describeElement ('The context used was' ),
73+ ]);
74+ }
3475
35- return streamChatConfiguration! .data;
76+ /// Finds the [StreamChatConfigurationData] from the closest
77+ /// [StreamChatConfiguration] ancestor that encloses the given context.
78+ ///
79+ /// Returns null if no such ancestor exists.
80+ ///
81+ /// See also:
82+ /// * [of] , which throws if no [StreamChatConfiguration] is found.
83+ static StreamChatConfigurationData ? maybeOf (BuildContext context) {
84+ final streamChatConfiguration =
85+ context.dependOnInheritedWidgetOfExactType <StreamChatConfiguration >();
86+ return streamChatConfiguration? .data;
3687 }
3788}
3889
0 commit comments