Device returns "Null check operator used on a null value" exception for a second, however i added checker to make sure value is not null. I am new in Flutter and i know this code is horrible, so can anyone gave some advices to make it more cleaner?
class _HotelPageState extends State<HotelPage> {
HotelMode? hotmod; // object of class
var isLoaded = false;
@override
void initState() {
super.initState();
getData();
}
getData() async {
hotmod = await HotelService().getHotelInfo();
if (hotmod != null) { // checks if it has data
setState(() {
isLoaded = true;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: backgroundbgtheme,
appBar: AppBar(
centerTitle: true,
title: const Text(
'Отель',
style: TextStyle(color: Colors.black),
),
toolbarHeight: 50,
backgroundColor: backgroundtheme,
elevation: 0,
),
body: Visibility(
replacement: const Center(
child: CircularProgressIndicator(),
),
visible: isLoaded,
child: ListView(
children: [ HotelScreen(
image: hotmod!.imageUrls[1],
hotelname: hotmod!.name,
price: hotmod!.minimalPrice,
priceForIt: hotmod!.priceForIt,
country: hotmod!.adress,
rating: hotmod!.rating,
ratingName: hotmod!.ratingName),
HotelDescription(hotdesc: hotmod!.aboutTheHotel.description),
const BottomButtonWidget()
]
),
)
);
}
}
3
Answers
You can handle loading and error for future methods with
FutureBuilder
.Create a Future method with return type HotelMode:
Use FutureBuilder for scaffold’s body:
the data you’re getting (** getHotelInfo**) is in Future. And UI will not wait until data comes.You should use FutureBuilder in this case. or make all parametrs of HotelScreen widget nullable. Here is how you can use FutureBuilder in your case:
Your problem is, that the
initState
method is not waiting on the result ofgetData
. It runs,getData
is not done yet, the widget itbuild
the first time and crashes, because for a specific time,hotmod
is null. Only later it is filled.So at least you need to make sure your build method does not crash when
hotmod
is null. Right now, it is invisible, but it still is build and crashes. So don’t build it if it is null:However, there is a solution already ready-made for this in flutter: flutter-futurebuilder
See the example at What is a Future and how do I use it?