user10583336
user10583336

Reputation: 447

initState in Flutter return null on SQLite database?

I create a button to test the database. it returns a set of data. While i did in initState function, what i get is null.

@override
void initState() {
  db.queryData('username').then((val) {
    _userAuthData = val;
  });
  super.initState();
  print(_userAuthData);
}```

** both async/await and future.then() works the same in button on press function.

Upvotes: 1

Views: 318

Answers (1)

Khadga shrestha
Khadga shrestha

Reputation: 1180

you can execute async function on init method by using future

@override
void initState() {
  super.initState();
  Future.delayed(Duration.zero,()async{       
   final _userAuthData=await db.queryData('username');  
   print(_userAuthData);
 });  
});

Upvotes: 2

Related Questions