0

I have home screen which two tabs. now when I get to home tab and got to search instantly. I get red screen for slight second and then all widgets get loaded.

Now where problem is,

in initState() I'm assigning store.filteredPOI to widget.floorPlan.pois.

I'm getting store.filteredPOI from a network call which will take some time. so in that fraction of time widget.floorPlan is null so how can I show a loader to prevent the red error screen,

code

class SearchTab extends StatefulWidget {
  final FloorPlan floorPlan;
  final bool isIndoorMapVisible;
  final NetworkStatus networkStatus;
  SearchTab({this.floorPlan, this.isIndoorMapVisible,this.networkStatus});

  @override
  _SearchTabState createState() => _SearchTabState();
}

class _SearchTabState extends State<SearchTab> {
  final TextEditingController textController = TextEditingController();
  SearchStore store;

  @override
  void initState() {
    store = SearchStore();
    store.filteredPOI = widget.floorPlan.pois; //<<<<<<<<
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return (...)
Pokaboom
  • 1,110
  • 9
  • 28
  • try to move super.initState(); at the first line and compile again. – khoi Jun 03 '21 at 11:42
  • return widget.floorPlan == null ? Text("Loading") : OtherWidget(...); – Afridi Kayal Jun 03 '21 at 11:44
  • @AfridiKayal Kayal actually the error is coming from initState itself. so it is not even reaching to build method. – Pokaboom Jun 03 '21 at 11:58
  • @khoi that doesn't work – Pokaboom Jun 03 '21 at 11:58
  • then you need to make sure that the value is a pass-first at variable floorPlan then call SearchTab(). Need to create a flag to identify if the value already receives from the network or not. I don't know your full code, but something like this ---> Scaffold( body: isReceiveValueFromNetwork ? SearchTab(floorPlan:floorPlan,..) : Text("loading.."); ); – khoi Jun 03 '21 at 12:06
  • 1
    Does this answer your question? [What is a Future and how do I use it?](https://stackoverflow.com/questions/63017280/what-is-a-future-and-how-do-i-use-it) – nvoigt Jun 03 '21 at 12:17
  • @Pokaboom What is the error message? – Afridi Kayal Jun 03 '21 at 13:28
  • @AfridiKayal error was `widget.floorPlan` is called on null but what ended up doing was added the the assignment operation in the build method so now it working fine – Pokaboom Jun 04 '21 at 04:50

1 Answers1

0

what I ended up doing was add the operation inside built method

 @override
  Widget build(BuildContext context) {
   store.filteredPOI = widget.floorPlan.pois;
    return (...)
Pokaboom
  • 1,110
  • 9
  • 28