2

I'm having a problem with null safety in Flutter, I want to put my data inside horizontal_data_table in Flutter, and I call it with StreamBuilder but when I call it inside the widget, it shows the error:

The property 'paidLeaveTypeName' can't be unconditionally accessed because the receiver can be 'null'. Try making the access conditional (using '?.') or adding a null check to the target ('!').

and this is the full code of the Column itself:

Widget _generateRightHandSideColumnRow(BuildContext context, int index) {
    return Row(
      children: <Widget>[
        Container(
          width: 200,
          height: 52,
          padding: const EdgeInsets.fromLTRB(5, 0, 0, 0),
          alignment: Alignment.centerLeft,
          child: StreamBuilder<dynamic>(
            stream: _leavelistbloc.DataStream,
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                switch (snapshot.data!.status!) {
                  case Status.INITIAL:
                    return Text('');
                  case Status.LOADING:
                    return Text('');
                  case Status.COMPLETED:
                    LeaveListModel responses =
                        snapshot.data!.data as LeaveListModel;
                    return Text(responses.response.paidLeaveTypeName);
                  case Status.ERROR:
                    return Text('');
                }
              }
              return Container();
            },
          ),
        ),
      ],
    );
  }
}

and this is exactly where the error code:

                case Status.COMPLETED:
                    LeaveListModel responses =
                        snapshot.data!.data as LeaveListModel;
                    return Text(responses.response.paidLeaveTypeName);

the model data of the result is look like this:

class LeaveListResult {
  int? paidLeaveId;
  String? paidLeaveUuid;
  String? paidLeaveTypeName;
  String? paidLeaveEmployeeUuid;
  String? paidLeaveEmployeeNip;
  String? paidLeaveEmployeeFullName;
}

but in case you're wondering what the full code of model looks like, you can see it on here is anyone know how to deal with this or how to call the data inside horizontal_data_table with using StreamBuilder?

1988
  • 309
  • 2
  • 15

1 Answers1

1

Try this way by checking null response

  final result  = responses.response?.result;
   if(result==null) Text("got null result");
   else if (result!.length > 0) {
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
  • accepting null with String format can be `Text("${widget.leavelistmodel.response?[index].paid}")` – Md. Yeasin Sheikh Nov 17 '22 at 03:39
  • i got the error: "The operator '[]' isn't defined for the type 'LeaveListResponse'. Try defining the operator '[]' " when i add '?' after leavelistmodel.response. I think my model is already correct. – 1988 Nov 17 '22 at 03:59
  • ok the model isnt nullable on top level , try `LeaveListModel? responses =snapshot.data?.data as LeaveListModel?;` – Md. Yeasin Sheikh Nov 17 '22 at 04:01
  • i still got the same error "The operator '[]' isn't defined for the type 'LeaveListResponse'. Try defining the operator '[]' ", i'm already change it into '?' – 1988 Nov 17 '22 at 04:10
  • what do you get from `snapshot.data!.data` – Md. Yeasin Sheikh Nov 17 '22 at 04:11
  • i'm actually want to make a condition so the data won't be null when it getting insert into the table, i have try the simple one without the streambuilder with the null checking condition like in my previous post but i still found it didn't fix at all, so i try to put it inside the stream builder but the null safety keeps showing. i made the conclusion that the only problem in here are when i call the data from the model inside the Text(widget.leavelistmodel.response[index].paid ?? ''); – 1988 Nov 17 '22 at 04:23
  • I think it is good to accept null value then doing a null check ,Are getting issue only on text? what do you get after using `Text("${widget.leavelistmodel.response?[index].paid}")` or for not null `Text("${widget.leavelistmodel.response[index].paid}")` – Md. Yeasin Sheikh Nov 17 '22 at 04:25
  • yes, it showing the same alert "The method '[]' can't be unconditionally invoked because the receiver can be 'null'. Try making the call conditional (using '?.') or adding a null check to the target ('!').", im already try to delete the stream builder and just put the text into child without any null check just like this "Text("${widget.leavelistmodel.response[index].paid}")" – 1988 Nov 17 '22 at 04:35
  • I think we are making mistake with response , while it is already available after fetch , try `responses[index].paid` not `widget.leavelistmodel.` – Md. Yeasin Sheikh Nov 17 '22 at 04:39
  • the error alert is back to "The operator '[]' isn't defined for the type 'LeaveListModel'. Try defining the operator '[]'." i really got confuse why the null safety keeps showing the error alert – 1988 Nov 17 '22 at 04:55
  • 1
    `LeaveListModel` is a single instance, not a list, lets say you a list inside this model, you can use [] on that filed – Md. Yeasin Sheikh Nov 17 '22 at 04:59
  • oh okay thank you i really fix the problem with that one with change it into responses.response.paidLeaveType, but i can't help with the null safety error that mention "The property 'paidLeaveType' can't be unconditionally accessed because the receiver can be 'null'. Try making the access conditional (using '?.') or adding a null check to the target ('!')." I'm trying to implement the previous null checker but still doesn't solve it, if you don't mind, do you know how to fix or add the null checker inside it? – 1988 Nov 17 '22 at 05:13
  • It means the second `response` might be nullable at this point try `responses.response?.paidLeaveType` – Md. Yeasin Sheikh Nov 17 '22 at 05:15
  • there's an error "The getter 'paidLeaveType' isn't defined for the type 'LeaveListResponse'. Try importing the library that defines 'paidLeaveType', correcting the name to the name of an existing getter, or defining a getter or field named 'paidLeaveType'." – 1988 Nov 17 '22 at 05:20
  • then it means you doesnt a filed on model class as named `paidLeaveType` – Md. Yeasin Sheikh Nov 17 '22 at 05:21
  • actually im having paidLeaveType in my model, im already checking it for twice – 1988 Nov 17 '22 at 06:39
  • Can you include the model class ? – Md. Yeasin Sheikh Nov 17 '22 at 06:40
  • yes, you can see my model on http://pastie.org/p/6IRsRokVMezry28YLwplk6 and my repository on http://pastie.org/p/1C8NQpqLU5t4tWqw0VsbwL – 1988 Nov 17 '22 at 06:50
  • 1
    `result` will be used which is also a nullable & while `LeaveListResponse` holding list of `LeaveListResponse`. Maybe you want `class LeaveListResponse { int? total; List? result;` – Md. Yeasin Sheikh Nov 17 '22 at 09:04
  • yes, i'm calling LeaveListResponse to the column of the datatable but somehow it can't be – 1988 Nov 18 '22 at 02:05