_showMessage of EzGlobalMessageWrapper did not provide context for Scaffold
class EzGlobalMessageWrapper extends StatefulWidget {
final Widget child;
EzGlobalMessageWrapper(this.child);
@override
_EzGlobalMessageWrapperState createState() = _EzGlobalMessageWrapperState();
}
class _EzGlobalMessageWrapperState extends State<EzGlobalMessageWrapper {
@override
void initState() {
super.initState();
EzBlocProvider.of<EzGlobalBloc(context)
.get<EzMessageBloc(EzMessageBloc)
.messageStream
.listen((msg) {
if (msg != null) {
_showMessage(msg);
}
});
}
@override
Widget build(BuildContext context) {
return widget.child;
}
void _showMessage(EzMessage message) {
Color color = Colors.grey;
switch (message.type) {
case EzMessageType.SUCCESS:
if (EzSettings.app() != null) {
if (StringUtils.isNotNullOrEmpty(
EzSettings.app()[EzSettingsKeys.KEY_MSG_SUCCESS_COLOR])) {
color =
Color(EzSettings.app()[EzSettingsKeys.KEY_MSG_SUCCESS_COLOR]);
} else {
color = Colors.green;
}
} else {
color = Colors.green;
}
break;
case EzMessageType.INFO:
if (EzSettings.app() != null) {
if (StringUtils.isNotNullOrEmpty(
EzSettings.app()[EzSettingsKeys.KEY_MSG_INFO_COLOR])) {
color = Color(EzSettings.app()[EzSettingsKeys.KEY_MSG_INFO_COLOR]);
} else {
color = Colors.blue;
}
} else {
color = Colors.blue;
}
break;
case EzMessageType.WARNING:
if (EzSettings.app() != null) {
if (StringUtils.isNotNullOrEmpty(
EzSettings.app()[EzSettingsKeys.KEY_MSG_WARNING_COLOR])) {
color =
Color(EzSettings.app()[EzSettingsKeys.KEY_MSG_WARNING_COLOR]);
} else {
color = Colors.orange;
}
} else {
color = Colors.orange;
}
break;
case EzMessageType.ERROR:
if (EzSettings.app() != null) {
if (StringUtils.isNotNullOrEmpty(
EzSettings.app()[EzSettingsKeys.KEY_MSG_ERROR_COLOR])) {
color = Color(EzSettings.app()[EzSettingsKeys.KEY_MSG_ERROR_COLOR]);
} else {
color = Colors.red;
}
} else {
color = Colors.red;
}
break;
case EzMessageType.DEBUG:
if (EzSettings.app() != null) {
if (StringUtils.isNotNullOrEmpty(
EzSettings.app()[EzSettingsKeys.KEY_MSG_DEBUG_COLOR])) {
color = Color(EzSettings.app()[EzSettingsKeys.KEY_MSG_DEBUG_COLOR]);
} else {
color = Colors.grey;
}
} else {
color = Colors.grey;
}
break;
default:
}
SnackBar bar =
SnackBar(content: Text(message.text), backgroundColor: color);
Scaffold.of(context)
..hideCurrentSnackBar()
..showSnackBar(bar);
message.displayed = true;
}
}
when I show message. I got an error.
E/flutter ( 4388): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: 'package:flutter/src/material/scaffold.dart': Failed assertion: line 1447 pos 12: 'context != null': is not true. [38;5;244mE/flutter ( 4388): #0 _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:46:39)[39;49m [38;5;244mE/flutter ( 4388): #1 _AssertionError._throwNew (dart:core-patch/errors_patch.dart:36:5)[39;49m [38;5;244mE/flutter ( 4388): #2 Scaffold.of[39;49m [38;5;248mE/flutter ( 4388): #3 _EzGlobalMessageWrapperState._showMessage[39;49m [38;5;248mE/flutter ( 4388): #4 _EzGlobalMessageWrapperState.initState.
[39;49m [38;5;244mE/flutter ( 4388): #5 _rootRunUnary (dart:async/zone.dart:1198:47)[39;49m [38;5;244mE/flutter ( 4388): #6 _CustomZone.runUnary (dart:async/zone.dart:1100:19)[39;49m [38;5;244mE/flutter ( 4388): #7 _CustomZone.runUnaryGuarded (dart:async/zone.dart:1005:7)[39;49m [38;5;244mE/flutter ( 4388): #8 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:357:11)[39;49m [38;5;244mE/flutter ( 4388): #9 _DelayedData.perform (dart:async/stream_impl.dart:611:14)[39;49m [38;5;244mE/flutter ( 4388): #10 _StreamImplEvents.handleNext (dart:async/stream_impl.dart:730:11)[39;49m [38;5;244mE/flutter ( 4388): #11 _PendingEvents.schedule. (dart:async/stream_impl.dart:687:7)[39;49m [38;5;244mE/flutter ( 4388): #12 _rootRun (dart:async/zone.dart:1182:47)[39;49m [38;5;244mE/flutter ( 4388): #13 _CustomZone.run (dart:async/zone.dart:1093:19)[39;49m [38;5;244mE/flutter ( 4388): #14 _CustomZone.runGuarded (dart:async/zone.dart:997:7)[39;49m [38;5;244mE/flutter ( 4388): #15 _CustomZone.bindCallbackGuarded. (dart:async/zone.dart:1037:23)[39;49m [38;5;244mE/flutter ( 4388): #16 _rootRun (dart:async/zone.dart:1190:13)[39;49m [38;5;244mE/flutter ( 4388): #17 _CustomZone.run (dart:async/zone.dart:1093:19)[39;49m [38;5;244mE/flutter ( 4388): #18 _CustomZone.runGuarded (dart:async/zone.dart:997:7)[39;49m [38;5;244mE/flutter ( 4388): #19 _CustomZone.bindCallbackGuarded. (dart:async/zone.dart:1037:23)[39;49m [38;5;244mE/flutter ( 4388): #20 _microtaskLoop (dart:async/schedule_microtask.dart:41:21)[39;49m [38;5;244mE/flutter ( 4388): #21 _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)[39;49m E/flutter ( 4388):
Hello @sinhphan
Can you provide an code example how you try to display the message ?
Did you use the EzScaffold class in your app ?
import 'package:ez_flutter/ez_flutter.dart';
import 'package:flutter/material.dart';
void main() async {
await EzRunner.run(
MyApp(),
'Hello World',
displayDebugBadge: false,
);
}
class MyApp extends StatelessWidget {
const MyApp({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return EzScaffold(
body: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return Center(
child: FlatButton(
onPressed: () {
EzBlocProvider.of<EzGlobalBloc>(context)
.get(EzMessageBloc)
.addition
.add(EzMessage('my first message', EzMessageType.SUCCESS));
},
child: Text('Click me'),
);
}
}
button and message worked but debug console give above message
I think _showMessage(EzMessage message) didn't have a context argument. inside this,
Scaffold.of(context)
..hideCurrentSnackBar()
..showSnackBar(bar);
context always null