Home > Mobile >  how to remove duplicate from list of objects in flutter?
how to remove duplicate from list of objects in flutter?

Time:08-22

I'm new to flutter. I'm working on small projects which is like a scanning QR app. Here, I used hive to store scanned data in box but the problem is, it is storing duplicate values. I need to remove that duplicate data how to do it?

code:

class PageState extends State<PassthroughQrScanPage> {
  final ApiRepository repository = ApiRepository(
    apiClient: ApiClient(
      httpClient: http.Client(),
    ),
  );
  @override
  void initState() {
    super.initState();
    openBox();

    Prefs().reload();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: BlocProvider(
        create: (context) => PassthroughqrscanBloc(repository),
        child: BlocConsumer<PassthroughqrscanBloc, PassthroughqrscanState>(
          listener: (context, state) {
            if (state is PassthroughqrscanEmpty) {
              return scan(context);
            }
            if (state is PassthroughqrscanError) {
              Navigator.pop(context);
              ShowErrorMessage(context, state.error.message.toString());
            }
            if (state is PassthroughqrscanLoaded) {
              List<Batch> cleared = [];
              state.entity.batches.forEach((element) {
                cleared.add(element);
              });

           
              // final clearedData =
              //     cleared.map((item) => jsonEncode(item)).toList();
              // final uniqueJsonList = clearedData.toSet().toList();
              // List result =
              //     uniqueJsonList.map((item) => jsonDecode(item)).toList();
              var seen = Set<int>();
              List<Batch> clearedData = cleared
                  .where((cleared) => seen.add(cleared.batchNumber!))
                  .toList();
              // clearedData = [
              //   ...{...clearedData}
              // ];

              clearedData.forEach((element) {
                debugPrint(
                    "check the values for all the sdasd ${element.batchNumber}");
                box.add(Batch(
                    batchNumber: element.batchNumber,
                    isUsed: element.isUsed,
                    transactionId: element.transactionId));
              });

              print("adding ssssss ${box.values.toList()}");

              // String json = jsonEncode(state.entity);
              // print("------>>>>>>>>>>>D>S>D>>$json");
              // Prefs().setPassthroughData(json);
              Navigator.pop(context);
              WidgetsBinding.instance.addPostFrameCallback((_) {
                showDialog(
                    context: context,
                    builder: (ctxDialog) => PassDialog(
                          compoundCode: widget.compoundCode.toString(),
                          lotNo: widget.lotNo.toString(),
                          schedule_id: widget.schedule_id.toString(),
                          screenClosed: _screenWasClosed,
                          scheduleRange: widget.scheduleRange,
                          batchQty: widget.batchQty,
                        ));
              });
            }
            // return Container();
            Center(
              child: CircularProgressIndicator(),
            );
          },
          builder: (context, state) {
            if (state is PassthroughqrscanEmpty) {
              return scan(context);
            } else
              return scan(context);
          },
        ),
      ),
    );
  }
  scan(BuildContext mcontext) =>
     
      MobileScanner(
          controller: MobileScannerController(facing: CameraFacing.back),
          allowDuplicates: false,
          onDetect: (barcode, args) {
            if (barcode.rawValue == null) {
              WidgetsBinding.instance.addPostFrameCallback((_) {
                ScaffoldMessenger.of(context).showSnackBar(SnackBar(
                    content: Text("Scan correct QR"),
                    duration: Duration(milliseconds: 800)));
              });
            } else {
              String code = barcode.rawValue ?? "";
              debugPrint('Barcode found! $code');

              if (code.isNotEmpty) {
                // if (!_screenOpened) {
                // _screenOpened = true;
                passthroughData = jsonDecode(code);
                passthroughQrScan =
                    PassthroughQrScanData.fromJson(passthroughData);
                BlocProvider.of<PassthroughqrscanBloc>(mcontext)
                  ..add(VerifyPassthroughBatch(
                      passthroughQrScan?.operationName ?? "",
                      widget.schedule_id.toString(),
                      passthroughQrScan?.transactionId ?? "",
                      passthroughQrScan?.transactionRange ?? ""));
                buildShowDialog(context);
              }
            }
          });
  Widget ShowErrorMessage(BuildContext context, String error) {
    print("------------------------------/./././$error");
    WidgetsBinding.instance.addPostFrameCallback((_) {
      ScaffoldMessenger.of(context).showSnackBar(SnackBar(
          content: Text("Scan correct QR"),
          duration: Duration(milliseconds: 800)));
    });
    return scan(context);
  }

  Future<void> openBox() async {
    box = await Hive.openBox<Batch>("GetBatches");
    // box = Boxes.getAllBatches();
    await box.clear();
    debugPrint("wwwwwwwwwwwwwwwwwkekekkkkkkkkk${box.values}");
    // await box.deleteAll(box.keys);
  }
 }
List<Batch> batched = [];
var data;

class PassDialog extends StatefulWidget {
  // const PassDialog({Key? key}) : super(key: key);
  String? schedule_id;
  String? compoundCode;
  String? lotNo;
  final Function() screenClosed;
  final String? scheduleRange;
  final int? batchQty;

  PassDialog(
      {required this.schedule_id,
      required this.compoundCode,
      required this.lotNo,
      required this.screenClosed,
      required this.scheduleRange,
      required this.batchQty});

  @override
  State<PassDialog> createState() => _PassDialogState();
}

class _PassDialogState extends State<PassDialog> {
  @override
  void initState() {
    batched = box.values.toSet().toList();
    print("got values check for $batched");

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    // List<Batch> batch = box.get("GetBatches");
    //Batch batch = box?.get("GetBatches");
    return SizedBox(
      width: 150,
      height: 100,
      child: AlertDialog(
        content: SingleChildScrollView(
          child: Column(
            children: [
              // batched.forEach((element) {
              for (final q in batched)
                Text(
                    '${q.batchNumber.toString()}--${q.isUsed.toString()}--${q.transactionId.toString()}'),
              // }),
              Row(
                children: [
                  ElevatedButton(
                      onPressed: () {
                        //  print(values);
                        widget.screenClosed();
                        Navigator.of(
                          context,
                          rootNavigator: true,
                        ).pop(
                          context,
                        );
                      },
                      child: Text("Continue")),
                  SizedBox(
                    width: 10,
                  ),
                  ElevatedButton(
                      onPressed: () {
                        print(widget.scheduleRange);
                        WidgetsBinding.instance.addPostFrameCallback((_) {
                          Navigator.push(
                              context,
                              new MaterialPageRoute(
                                  builder: (_) => GluePassthroughUploadPage(
                                        id: widget.schedule_id.toString(),
                                        compoundCode:
                                            widget.compoundCode.toString(),
                                        lotNo: widget.lotNo.toString(),
                                        scheduleRange: widget.scheduleRange,
                                        batchQty: widget.batchQty,
                                        // getAllBatch: getBatch,
                                      )));
                        });
                      },
                      child: Text("Show Add page")),
                ],
              ),
            ],
          ),
        ),
        // Text("hohohoooooo${batched[0].batchNumber}${batched[0].isUsed}"),
      ),
    );
  }
}

Future buildShowDialog(BuildContext context) {
  return showDialog(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return Center(
          child: CircularProgressIndicator(),
        );
      });
}

How to remove duplicate list of objects? I'm using Batch as model class. I tried many methods to solve this issue. toSet(), like that.......

CodePudding user response:

If you want dealing with more complex objects, store seen ids to the Set and filter away those ones that are already in the set.

final list = ['a', 'a', 'b', 'c', 'c'];
final seen = <String>{};
final uniqueList = list.where((str) => seen.add(str)).toList();
print(uniqueList); // => ['a', 'b', 'c']

CodePudding user response:

With the help of this, You can easily get Unique data from list and it will remove duplicate value

uniqueList = uniqueList.toSet().toList();

CodePudding user response:

For your Batch Model, you can filter with a function like this:

 List<Batch> removeDuplicates(List<Batch> items) {
  List<Batch> uniqueItems = []; // uniqueList
  var uniqueIDs = items
      .map((e) => e.uniqueID)
      .toSet(); //list if UniqueID to remove duplicates
  uniqueIDs.forEach((e) {
    uniqueItems.add(items.firstWhere((i) => i.uniqueID == e));
  }); // populate uniqueItems with equivalent original Batch items
  return uniqueItems;//send back the unique items list
 }

Replace uniqueID with the parameter you want to use for filtering duplicates

  • Related