Call asynchronous function inside constructor

pz7

I have a widget where an attribute is obtained through a query on a database. So this function has to be marked as asynchronous, but i can't call it inside a constructor. Does someone know how to solve this ?

class PositionWidget extends StatefulWidget {
   final String streetName;
   String date;
   List<Map> notification;
   final VoidCallback onDelete;

  PositionWidget({Key key, this.streetName, this.date,this.notification, @required this.onDelete}): super(key: key){
    this.notification = await DBHelper.instance.getNotification(this.streetName);
  }
}


Future<List<Map>> getNotification(String street) async {
    Database db = await DBHelper.instance.database;
    var res = await db.query(table, columns: [columnNot], where: '${DBHelper.columnName} = ?', whereArgs: [street]);
    return res;
}

F Perroch

You can't do this inside the constructor of your StatefulWidget

You need to make your call inside the the initState() method of the PositionWidgetState (the State of your PositionWidget

class PositionWidgetState extends State<PositionWidget> {
  var res;

  @override
  void initState() {
    res = getNotification(widget.streetName);
  }

You'll probably need to a FutureBuilder or StreamBuilder to deal with the result

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Call asynchronous method in constructor?

Call function inside constructor of component in react?

Javascript call prototype function from function inside constructor

Call an asynchronous function with react

Asynchronous Function Call

Call asynchronous function recursively

Call a virtual function inside the constructor using an object-expression

Is it possible to call an async function inside a constructor in react-native?

Can I make a local function inside the constructor and call it

Asynchronous call inside a javascript for loop

Call constructor inside a call to another constructor

How to create a Javascript (es5) constructor function containing an asynchronous call

Using asynchronous call in class javascript class constructor

call function at extends constructor

Call function in constructor

Waiting for Asynchronous function call to complete

How to call an asynchronous JavaScript function?

Call An Asynchronous Javascript Function Synchronously

How to recursively call an asynchronous function?

Argument lifetime of an asynchronous function call

Nodejs Asynchronous reusable function call

Wrap Asynchronous Function Call In an NSOperation

Is it possible to put a constructor function inside a constructor function inside another constructor?

Arrow function and this inside a constructor function

Asynchronous call inside Meteor.onCreateUser hook

Exporting a function declared inside asynchronous function

Unable to call non-static constructor from inside static main function

Call a function inside promise

Call a function inside a if condition