Sanchay Kasturey
Sanchay Kasturey

Reputation: 341

How to pass arguments to a function in flutter?

I am having the following function:

Future<List<expense>> getExpenseDateWise(DateTime FromDate, DateTime ToDate) async {
final db = await database;
var expenses = await db
    .rawQuery('SELECT * FROM EXPENSES WHERE DATE(DATETIME) >= ? AND DATE(DATETIME) <= ?',
['$FromDate','$ToDate']);

List<expense> expensesList = List<expense>();
expenses.forEach((currentExpense) {
  expense expenses = expense.fromMap(currentExpense);
  expensesList.add(expenses);
});
return expensesList;
}

And the above function requires 2 arguments.

I am calling the above function in a following way:

 class dateWiseList extends StatefulWidget {
  final DateTime fromDate;
  final DateTime toDate;

  dateWiseList({this.fromDate, this.toDate});
 @override
 _dateWiseListState createState() => _dateWiseListState();
 }


class _dateWiseListState extends State<dateWiseList> {
@override
void initState() {
super.initState();
DatabaseProvider.db.getExpenseDateWise(// <----------1----------> //).then(
      (expenseList) {
    BlocProvider.of<ExpenseBloc>(context).add(SetDates(expenseList));
  },
 );
}

In <----------1----------> I need to pass the two arguments. I don't know how to do it please help me.

The function getExpenseDateWise is used for fetching data records between two dates selected by the user in a different form. That's why I have called those values using dateWiseList({this.fromDate, this.toDate});. Thanks for your replies

Upvotes: 1

Views: 712

Answers (2)

Simeon
Simeon

Reputation: 781

Try widget.fromDate and widget.toDate. Coz I'm seeing you're using a stateful widget.

If your function takes positional arguments, eg Future doSomething(String name, Bool jerk) { Lots of code }

Then when calling it inside a stateful widget, while using some arguments you passed to that widget, do tgis blah blah = doSomething(widget.argument1, widget.argument2)

If it's not positional arguments, eg Future doSomething({String name, Bool jerk} ) { Lots of code }

, then it's gonna be blah blah = doSomething( name: widget.argument1, jerk: widget.argument2)

Upvotes: 1

Ezaldeen sahb
Ezaldeen sahb

Reputation: 739

You just need to pass the starting date and the end date to filter your data

DateTime now = DateTime.now();
DtaeTime yesterday = DateTime.now().subtract(Duration(days:1));
DatabaseProvider.db.getExpenseDateWise(yesterday, now).then(.........)

you can treat flutter functions just like any other functions

Upvotes: 1

Related Questions