1

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

this .dbHelper .getExpensesByFundId(this._selectedFund.id) .then((列出费用) { setState(() { this._expenses = costs; 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,
      ),
    );
  }

}
4

0 回答 0