thingsboard / dart_thingsboard_pe_client

ThingsBoard PE API client library for Dart developers.
BSD 3-Clause "New" or "Revised" License
49 stars 16 forks source link

Null issues in getDeviceService().getUserDevices(PageLink(1)) #3

Closed Dmuldoraf closed 1 year ago

Dmuldoraf commented 2 years ago

Hi, I faced the following error, while try calling _tbClient.getDeviceService().getUserDevices(PageLink(1)): E/flutter ( 5652): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type 'Null' is not a subtype of type 'Map<String, dynamic>' E/flutter ( 5652): #0 new Device.fromJson package:thingsboard_pe_client/…/model/device_models.dart:766 E/flutter ( 5652): #1 parseDevicePageData.<anonymous closure> package:thingsboard_pe_client/…/service/device_service.dart:8 E/flutter ( 5652): #2 dataFromJson.<anonymous closure> package:thingsboard_pe_client/…/page/page_data.dart:35 E/flutter ( 5652): #3 MappedListIterable.elementAt (dart:_internal/iterable.dart:413:31) E/flutter ( 5652): #4 ListIterator.moveNext (dart:_internal/iterable.dart:342:26) E/flutter ( 5652): #5 new _GrowableList._ofEfficientLengthIterable (dart:core-patch/growable_array.dart:206:27) E/flutter ( 5652): #6 new _GrowableList.of (dart:core-patch/growable_array.dart:153:28) E/flutter ( 5652): #7 new List.of (dart:core-patch/array_patch.dart:51:28)

While debugging, I recieved this: in device_model.dart deviceProfileIdIsNull

With the following runtime variables: data

Most of the devices parameter are empty, because I created it just with the necessary params with the API. How can I use the method _tbClient.getDeviceService().getUserDevices(PageLink(1)) with this kind of devices?

Best regards :)

vvlladd28 commented 2 years ago

Hi @Dmuldoraf, This is an exciting issue. Can you tell in more detail which version of the TB platform is installed in the server where you connect the app? Because deviceProfileId is the required field.

drausal commented 2 years ago

Ran into this issue as a "CUSTOMER_USER" not "TENANT_ADMIN" the fix was to check if its null

This was required for both deviceProfileId and deviceData

Device.fromJson(Map<String, dynamic> json)
       : tenantId = TenantId.fromJson(json['tenantId']),
         customerId = json['customerId'] != null
             ? CustomerId.fromJson(json['customerId'])
             : null,
         name = json['name'],
         type = json['type'],
         label = json['label'],
         deviceProfileId = json['deviceProfileId'] != null ? DeviceProfileId.fromJson(json['deviceProfileId']) : null,
         firmwareId = json['firmwareId'] != null
             ? OtaPackageId.fromJson(json['firmwareId'])
             : null,
         softwareId = json['softwareId'] != null
             ? OtaPackageId.fromJson(json['softwareId'])
             : null,
         deviceData = json['deviceData'] != null ? DeviceData.fromJson(json['deviceData']) : DeviceData(),
         externalId = json['externalId'] != null
             ? DeviceId.fromJson(json['externalId'])
             : null,
         super.fromJson(json);
Server: Ubuntu
TB Version: PE 3.4 
Flutter: 3.0.5
Dart: 2.17.6