2

我无法在这里使用未来布尔值..有什么方法可以在这种情况下使用未来布尔值..或者有没有更好的方法来做到这一点?

 Widget _buildRow(String pair) {


    final Future<bool> alreadySaved = DBHelper.getAllEmployees().then((value) => value.contains(pair));
    print("Already saved $alreadySaved");
    print(pair);
    return FutureBuilder(
      future: DBHelper.getAllEmployees(),
      builder: (context, AsyncSnapshot<List<FavrtTableModel>> snapshot) =>
      snapshot.connectionState == ConnectionState.waiting ? Center(
        child: Icon(Icons.more_horiz),
      ):IconButton(
        icon: new  Icon(
          alreadySaved ? Icons.favorite : Icons.favorite_border,
          color:alreadySaved? Colors.red : Colors.white,
        ),onPressed: (){
        setState(() {
          if (alreadySaved) {
            _saved.remove(pair);
            _deleteEmployee(pair);
          } else {
            _saved.add(pair);
            _insert(pair);


          }
        });
      },
      ),
    );
  }
4

1 回答 1

1

initState()像这样使用

 bool alreadySaved = false;

  @override
  void initState() {
    super.initState();
    String pair = "pair_value";
    DBHelper.getAllEmployees().then((value) {
      setState(() {
        alreadySaved = value.contains(pair);
      });
    });
  }
于 2020-09-14T15:55:35.870 回答