8

Is there any way I can use a future bool in this type of condition, or is there a better way to do this?

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);
                    }
                });
            },
        ),
    );
}
user4157124
  • 2,809
  • 13
  • 27
  • 42
Adrita Rahman
  • 159
  • 1
  • 1
  • 13

1 Answers1

3

Use in initState() like

 bool alreadySaved = false;

  @override
  void initState() {
    super.initState();
    String pair = "pair_value";
    DBHelper.getAllEmployees().then((value) {
      setState(() {
        alreadySaved = value.contains(pair);
      });
    });
  }
Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147
  • i thought of that.. but the problem is in the pair value.. it cannot be shifted to initstate(),.. actually the pair value comes from a future builder in another widget,. – Adrita Rahman Sep 14 '20 at 23:57
  • That's ok but you can move this logic to another widget class? and pass pair value to that widget. or maybe you can create stateful widget for that and use initState() like my answer – Jitesh Mohite Sep 15 '20 at 04:18