Чак Джонс
Чак Джонс

Reputation: 135

Compare two lists with String data?

How I can compare two lists with String data?

I need if values in second list match with first, change icon to red, if not match to green.

isEqual ? Colors.red : Colors.green

First list

List<String> pcAll = ['S01', 'S02', 'S03', 'S04', 'S05'];

Second list

List<String> pcBusy = ['S02', 'S03'];
class ComputerGrid extends StatelessWidget {
  const ComputerGrid();

  @override
  Widget build(BuildContext context) {
    return GridView.builder(
      gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
        maxCrossAxisExtent: 200,
        childAspectRatio: 6 / 3,
        crossAxisSpacing: 20,
        mainAxisSpacing: 20,
      ),
      itemCount: pcAll.length,
      itemBuilder: (BuildContext context, index) {
        return GridTile(
          child: Container(
            color: isEqual() ? Colors.red : Colors.green,
            child: Center(
              child: Text(
                pcAll[index],
              ),
            
class _EvrokoStandartScreenState extends State<EvrokoStandartScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text(
          'ЕВРОКО Стандарт',
        ),
      ),
      body: ComputerGrid(),
 

Upvotes: 0

Views: 1977

Answers (3)

Maulik Sinroja
Maulik Sinroja

Reputation: 191

@Чак-Джонс you can compare the list as below.

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  List<String> pcAll = ['S01', 'S02', 'S03', 'S04', 'S05'];
  List<String> pcBusy = ['S02', 'S03'];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: GridView.builder(
        gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
          maxCrossAxisExtent: 200,
          childAspectRatio: 6 / 3,
          crossAxisSpacing: 20,
          mainAxisSpacing: 20,
        ),
        itemCount: pcAll.length,
        itemBuilder: (BuildContext context, index) {
          return GridTile(
            child: Container(
              color: pcBusy.contains(pcAll[index]) ? Colors.red : Colors.green,
              child: Center(
                child: Text(pcAll[index]),
              ),
            ),
          );
        },
      ),
    );
  }
}

Upvotes: 1

Md. Yeasin Sheikh
Md. Yeasin Sheikh

Reputation: 63649

For listEqual

import 'package:flutter/foundation.dart';
...............
 bool isEqual = listEquals(pcAll, pcBusy);

For DeepCollectionEquality

import 'package:collection/collection.dart';

.......


    Function deepEq = const DeepCollectionEquality().equals;

    bool idDeepEqual = deepEq(pcAll, pcBusy);

Normal hardCoded

 List<String> pcAll = [
    'S01',
    'S02',
    'S03',
    'S04',
    'S05',
    'S06',
    'S07',
    'S09',
    'S10'
  ];

  List<String> pcBusy = ['S02', 'S03', 'S05', 'S06', 'S07', 'S08'];

  List<String> resultSet1 = [];

  pcBusy.forEach((pc) {
    if (pcAll.contains(pc)) resultSet1.add(pc);
  });

  print(resultSet1);
  print(resultSet1.length > 0 ? "didnot match" : "match");
     

Upvotes: 0

Kamal Thennakoon
Kamal Thennakoon

Reputation: 1829

You can compare two lists in flutter using listEquals method, or create a custom method like this.

bool areListsEqual(var list1, var list2) {
    // check if both are lists
    if(!(list1 is List && list2 is List)
        // check if both have same length
        || list1.length!=list2.length) {
        return false;
    }
     
    // check if elements are equal
    for(int i=0;i<list1.length;i++) {
        if(list1[i]!=list2[i]) {
            return false;
        }
    }
     
    return true;
}
 
void main(){
    List list1 = [24, 'Hello', 84];
    List list2 = [24, 'Hello', 84];
    List list3 = [11, 'Hi', 41];
     
    if(areListsEqual(list1, list2)) {
        print('list1 and list2 are equal in value.');
    } else {
        print('list1 and list2 are not equal in value.');
    }
     
    if(areListsEqual(list1, list3)) {
        print('list1 and list3 are equal in value.');
    } else {
        print('list1 and list3 are not equal in value.');
    }
}

Upvotes: 0

Related Questions