Axen_Rangs
Axen_Rangs

Reputation: 427

App is working fine but when app starts suddenly shows up this screen and disappear

I think problem is cause by this following function which supposed to change background of the function.How can I solve this.

_decidebg(){
   if(_counter==0){
     return AssetImage("assets/1.jpg");
   }
   else if(_counter<3){
     return AssetImage("assets/2.jpg");
   }
   else if(_counter<=6){
     return AssetImage("assets/3.jpg");
   }
   else{
     return AssetImage("assets/4.jpeg");
   }
 }

Screenshot

Upvotes: 0

Views: 50

Answers (2)

Sukhi
Sukhi

Reputation: 14205

The problem occurs because you are deriving the value after reading something from the disk. And it takes a few miliseconds to read from the disk. By that time, the value for variable _counter remains null and it shows the red screen then.

To solve the issue, initialise _counter with value say -1 (or whatever works with the logic). And wrap the assignment in setState(). So, something like :

From :

_counter = await _______ ;

To :

int _counter = -1;
...
...
setState() => _counter = await _______ ;

Upvotes: 1

timilehinjegede
timilehinjegede

Reputation: 14053

You are getting the error because the variable _counter is null.

You can solve this by given a default value to the _counter variable.

I hope this answers. your question.

Upvotes: 1

Related Questions