0

at value: await statusonoff(device), I am getting an error saying The argument type 'Future' can't be assigned to the parameter type 'bool'. when i retrive a bool from the firestore


  static Future<bool>statusonoff(Device device) async {
    DocumentSnapshot variable = await Firestore.instance
        .collection('device-configs')
        .document(device.id)
        .get();
    return variable.data['value']['on'];
  }

CustomSwitch(
                    activeColor: Colors.green,
                    value: await statusonoff(device),
                    onChanged: (value) async {
                      DocumentSnapshot variable = await Firestore.instance
                          .collection('device-configs')
                          .document(device.id)
                          .get();
                      print(variable.data['value']['on']);
                      setState(() {
                        if (variable.data['value']['on'] == false) {
                          value = true;
                          _configMode = "on";
                          _updateDeviceConfig(context, device);
                        } else if (variable.data['value']['on'] == true) {
                          value = false;
                          _configMode = "off";
                          _updateDeviceConfig(context, device);
                        }
                        status = value;
                      });
                    },
                  ),

1 Answers1

1

If you want to use a boolean retrieved from a Future, you need to use a FutureBuilder.

return FutureBuilder<bool>(
//! remove the parameters from statusonoff (Device device)
// or you could make it future:statusonoff(device)
//istantiate this instance of Device before you call it
  future:statusonoff,
  builder: (context, variable){
      if(variable.id.isNotEmpty)
       return CustomSwitch(
      //*put the rest of your code here  
      );
    }
);
Ferdinand
  • 513
  • 1
  • 3
  • 9