Flutter:setState()函数未正确更新值

时间:2019-02-04 04:33:46

标签: flutter statefulwidget

我有一个Flutter应用程序,其中在数据库查询方法之后立即在loadExpenses()函数内部调用setState()函数。 最初,费用列表的计数(expCount)设置为0。随后,根据资金的选择,费用列表被更新,因此expCount也被更新,以便ListView.builder()可以正确地更新listview。但是,我可以观察到从Fund dropDownMenu中选择了资金后,在loadExpenses()函数中正确地更新了费用清单。但是this.expCount = _expenses.length;不更新expCount值,这就是ListView Builder无法创建列表的原因。

  

这个           .dbHelper           .getExpensesByFundId(this._selectedFund.id)           .then((清单费用){         setState((){           this._expenses =支出;           this.expCount = _expenses.length;

 });

但是,如果我把那行放在setState()函数之外,我可以看到expCount变量正在更新,只是找到。你能告诉我这里是什么问题吗?

这是我的代码:

import 'package:flutter/material.dart';
import 'package:ksk_tavel_exp/Models/expense.dart';
import 'package:ksk_tavel_exp/Models/fund.dart';
import 'package:ksk_tavel_exp/utils/dbHelper.dart';

class ExpenseList extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return ExpenseListState();
  }
}

class ExpenseListState extends State<ExpenseList> {
  List<Fund> _funds;
  List<Expense> _expenses;
  Fund _selectedFund;
  int expCount = 0;

  DatabaseHelper dbHelper = DatabaseHelper();

  loadFunds() {
    if (this._selectedFund == null) {
      this.dbHelper.getFunds().then((List<Fund> funds) {
        setState(() {
          this._funds = funds;
          this._selectedFund = this._funds[0];
          this.loadExpenses();
        });
      });
    }
  }

  loadExpenses() {
    this
        .dbHelper
        .getExpensesByFundId(this._selectedFund.id)
        .then((List<Expense> expenses) {
      setState(() {
        this._expenses = expenses;
        this.expCount = _expenses.length;

     });
    });

  }

  Widget expenseBuilder(BuildContext context, int index) {
    return ListTile(
      leading: Text(this._expenses[index].date),
      title: Text(this._expenses[index].expense),
      trailing: Text('${this._expenses[index].amount} BDT'),
    );
  }

  @override
  Widget build(BuildContext context) {
    double screenHeight = MediaQuery.of(context).size.height;
    this.loadFunds();
//    this.loadExpenses();

    return Scaffold(
      appBar: AppBar(
        title: Text('KSK Expense App'),
        elevation: 1.0,
      ),
      body: Column(
        children: <Widget>[
          this._funds != null && this._selectedFund != null
              ? getFunds(screenHeight)
              : Container(),
          this._expenses != null ? this.getExpenses(screenHeight) : Container()
        ],
      ),
    );
  }

  Container getFunds(double screenHeight) {
    return Container(
      height: screenHeight*0.05,
      child: ListTile(
        leading: Text('Fund'),
        title: DropdownButton<Fund>(
          items: _funds.map((Fund item) {
            return DropdownMenuItem<Fund>(
              value: item,
              child: Text(item == null ? '' : item.fundName),
            );
          }).toList(),
          onChanged: (Fund selectedFund) {
            this._selectedFund = selectedFund;
            this.loadExpenses();
            this.expCount = _expenses.length;
          },
        ),
      ),
    );
  }

  Widget getExpenses(double screenHeight) {
    return Container(
        height: screenHeight*0.80,
      child: ListView.builder(
        itemBuilder: this.expenseBuilder,
        itemCount: this.expCount,
      ),
    );
  }

}

0 个答案:

没有答案