Home > OS >  Flutter searching through list error while using firstWhere, orElse
Flutter searching through list error while using firstWhere, orElse

Time:02-25

I have following model

import 'dart:convert';

ModelDevices modelDevicesFromJson(String str) =>
    ModelDevices.fromJson(json.decode(str));

String modelDevicesToJson(ModelDevices data) =>
    json.encode(data.toJson());

class ModelDevices {
  ModelDevices({
    required this.status,
    required this.message,
    required this.data,
  });

  String status;
  String message;
  List<BtData> data;

  factory ModelDevices.fromJson(Map<String, dynamic> json) =>
      ModelDevices(
        status: json["status"],
        message: json["message"],
        data: List<BtData>.from(json["data"].map((x) => BtData.fromJson(x))),
      );

  Map<String, dynamic> toJson() => {
        "status": status,
        "message": message,
        "data": List<dynamic>.from(data.map((x) => x.toJson())),
      };
}

class BtData {
  BtData({
    required this.id,
    required this.name,
    required this.serialNumber,
    required this.created,
    required this.lastEdited,
    required this.description,
    required this.ownerId,
    required this.dataId,
    required this.lastKnownCoordinates,
    required this.dateOfCoordinates,
    required this.lastDistance,
  });

  String id;
  String name;
  String serialNumber;
  String created;
  String lastEdited;
  String description;
  String ownerId;
  String dataId;
  String lastKnownCoordinates;
  String dateOfCoordinates;
  String lastDistance;

  factory BtData.fromJson(Map<String, dynamic> json) => BtData(
        id: json["id"],
        name: json["Name"],
        serialNumber: json["SerialNumber"],
        created: json["Created"],
        lastEdited: json["LastEdited"],
        description: json["Description"] ?? "",
        ownerId: json["OwnerID"],
        dataId: json["DataID"],
        lastKnownCoordinates: json["LastKnownCoordinates"] ?? "",
        dateOfCoordinates: json["DateOfCoordinates"] ?? "",
        lastDistance: json["LastDistance"] ?? "",
      );

  Map<String, dynamic> toJson() => {
        "id": id,
        "Name": name,
        "SerialNumber": serialNumber,
        "Created": created,
        "LastEdited": lastEdited,
        "Description": description,
        "OwnerID": ownerId,
        "DataID": dataId,
        "LastKnownCoordinates": lastKnownCoordinates,
        "DateOfCoordinates": dateOfCoordinates,
        "LastDistance": lastDistance,
      };
}

And in my controller

List<BtData> userDevices = <BtData>[];

I am searching the list like this

var getSavedDeviceFromResult = userDevices.firstWhere(
            (itm) => itm.serialNumber.toString() == r.device.id.toString(),
            orElse: () => null);

But i get following error.

Unhandled Exception: type '() => Null' is not a subtype of type '(() => BtData)?' of 'orElse'

How do I cope with this situation without using collection package?

Thanks

CodePudding user response:

you can't return null unless you change the type of your List to :

List<BtData?> userDevices = <BtData?>[];

CodePudding user response:

You can use fisrtOrNull

as

var getSavedDeviceFromResult = userDevices.fisrtOrNull(
            (itm) => itm.serialNumber.toString() == r.device.id.toString());

Do not forget to import collection

import 'package:collection/collection.dart';
  • Related