You understand better when see the code and error.
I have a StatelessWidget names StateWidget like this:
class StateWidget extends StatelessWidget {
const StateWidget({
super.key,
required this.readyWidget,
this.error = false,
this.errorWidget,
this.loading = false,
this.loadingWidget,
});
final Widget readyWidget;
final bool error;
final Widget? errorWidget;
final bool loading;
final Widget? loadingWidget;
@override
Widget build(BuildContext context) {
if (loading) {
return loadingWidget ?? LoadingCircularProgressIndicatorWidget();
}
if (error) {
return errorWidget ?? ErrorIconWidget();
}
if (!loading && !error) return readyWidget;
return Container();
}
}
And I have a StatefulWidget named StateWidgetExampleView like this:
class StateWidgetExampleView extends StatefulWidget {
const StateWidgetExampleView({super.key});
@override
State<StateWidgetExampleView> createState() => _StateWidgetExampleViewState();
}
class _StateWidgetExampleViewState extends State<StateWidgetExampleView> {
bool loading = true;
String? data;
@override
void initState() {
super.initState();
Future.delayed(Duration(milliseconds: 1000)).then((value) {
data = "Gelen Veri!";
setState(() {
loading = false;
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("StateWidgetExampleView"),
),
body: StateWidget(
loading: loading,
error: data == null,
readyWidget: Text(data!),
),
);
}
}
I get a sample data in initState function in 1 seconds. You guess that "loadingWidget appears because of loading is true". This idea is kind of true but it throws error like Null check operator used on a null value
. I think this renders the all widgets, then checks the conditions. Let’s look at the error and wait 1 seconds then our StateWidget is ready. I want a structure that I can control error, loading and ready widgets. But it throws like this error. How can I build the structure that I want?
3
Answers
I changed it "readyWidget" to "readyBuilder". So, I did it a function returns widget. Now there is no error, it's like I want! ^^
StateWidgetExampleView:
StateWidget:
The
StateWidget
widget is being creating from 1st frame. Even though you’ve waited some frame to handle null. ButThis
So the
Text
widget doesn’t accept null and while using null-assert, it is throwing the error.You’ve hidden the error on UI, not on compiler because
Text()
widget is creating from 1st frame.You can provide default value like
readyWidget: Text(data??"")
.You can achieve the desired functionality by using a FutureBuilder widget.