casting int to bool in dart
Asked Answered
C

7

11

I'm having a list of different types of values exported from JSON.

class StudentDetailsToMarkAttendance {
  int att_on_off_status;
  String name;
  String reg_number;
  int status;

  StudentDetailsToMarkAttendance(
      {this.att_on_off_status, this.name, this.reg_number, this.status});

  factory StudentDetailsToMarkAttendance.fromJson(Map<String, dynamic> json) {
    return StudentDetailsToMarkAttendance(
      att_on_off_status: json['att_on_off_status'],
      name: json['name'],
      reg_number: json['reg_number'],
      status: json['status'],
    );
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['att_on_off_status'] = this.att_on_off_status;
    data['name'] = this.name;
    data['reg_number'] = this.reg_number;
    data['status'] = this.status;
    return data;
  }
}

I am trying to use the value of status as the value parameter of Checkbox. I am trying to parse int to String like this.

value:((widget.studentDetailsList[index].status = (1 ? true : false) as int)as bool)

but there seems to be a problem with this conversion. I am not getting exact way of converting int to bool in dart. It says

Conditions must have a static type of 'bool'.

Catachresis answered 16/6, 2020 at 4:20 Comment(0)
U
25

To convert int to bool in Dart, you can use ternary operator :

myInt == 0 ? false : true;

To convert bool to int in Dart, you can use ternary operator :

myBool ? 1 : 0;
Underbodice answered 14/11, 2020 at 15:44 Comment(1)
Shorter answers for int to bool: myInt == 1Yellowthroat
I
6

I know this is an old question, but I think this is a clean way to convert from int to bool:

myBool = myInt.isOdd;

Or with null safety

myBool = myInt?.isOdd ?? false;
Infundibulum answered 24/8, 2021 at 2:12 Comment(0)
M
4

There is no way to automatically "convert" an integer to a boolean.

Dart objects have a type, and converting them to a different type would mean changing which object they are, and that's something the language have chosen not to do for you.

The condition needs to be a boolean, and an integer is-not a boolean.

Dart has very few ways to do implicit conversion between things of different type. The only real example is converting a callable object to a function (by tearing off the call method), which is done implicitly if the context requires a function. (Arguably an integer literal in a double context is "converted to double", but there is never an integer value there. It's parsed as a double.)

So, if you have an integer and want a bool, you need to write the conversion yourself. Let's assume you want zero to be false and non-zero to be true. Then all you have to do is write myInteger != 0, or in this case:

value: widget.studentDetailsList[index].status != 0
Milone answered 16/6, 2020 at 8:39 Comment(0)
F
2

Try using a getter.

bool get status {
    if(widget.studentDetailsList[index].status == 0)
              return false;
    return true;
}

Then pass status to value.

value: status
Fiddling answered 16/6, 2020 at 9:2 Comment(0)
I
0

Try this:

value: widget.studentDetailsList[index].status == 1
Illuminati answered 16/6, 2020 at 4:33 Comment(1)
? true : false is redundant.Constrain
N
0

I've just published a lib to convert any object to a bool value in dart, asbool (Disclaimer: I'm the author)

For int objects you can use it as a extension (.asBool) or helper method (asBool(obj)):


int? num = 23;
int? num2;

assert(num.asBool == true); // true == true
assert(asBool(num) == true); // true == true
assert(num2.asBool == false); // false == false
assert(0.asBool == asBool(null)); // false == false
assert(120.asBool == asBool(2.567)); // false == false

It also works for whatever other object like Strings, Iterables, Maps, etc.

Nanci answered 25/6, 2022 at 19:10 Comment(0)
T
0

If you need to use this often, you can go with creating an extension:

extension BoolExtensions on bool {
  int toInt() {
    return this ? 1 : 0;
  }
}

Then it can be used as:

boolVal.toInt()
Tympan answered 25/9, 2023 at 18:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.