Flutter 如何比较颤振(dart)中的两个列表数据

Flutter 如何比较颤振(dart)中的两个列表数据,flutter,dart,Flutter,Dart,我的页面上有一个网格数据。我想将这些数据与另一个列表(ID1,2,3,4,5)进行比较 使用数据ID比较上述两个列表 coursedata['courseid'] == course[index]['id']) ? "Added" : "" 请帮助我们比较网格视图生成器中的数据。目前,只有一个数据显示“已添加”,但也有其他数据未在视图中显示“已添加”。该行 Text((coursedata['courseid'] == course[index]['id']) ? "Added" : ""

我的页面上有一个网格数据。我想将这些数据与另一个列表(ID1,2,3,4,5)进行比较

使用数据ID比较上述两个列表

  coursedata['courseid'] == course[index]['id']) ? "Added" : ""
请帮助我们比较网格视图生成器中的数据。目前,只有一个数据显示“已添加”,但也有其他数据未在视图中显示“已添加”。

该行

Text((coursedata['courseid'] == course[index]['id']) ? "Added" : ""), 
只比较一个值,因为您只迭代一个列表。尝试通过迭代两个列表来调用返回布尔值的方法或文本小部件。如果函数仅返回false,则将添加到列表中。以下是返回文本小部件的sudo代码示例:

_ComparingLists(int id) {
bool temp = false;
  for (int i = 0; i < coursedata['courseid'].length; i++) {
  if ((coursedata['courseid'][i] == id)) {
  temp = true;
  break;
} else {
  temp = false;
}
 }

 // student is already enrolled
 if (temp == true) {
   return Text("Student is enrolled ...");
  }
  // student is not enrolled
  else {
  // do your operations like adding to the list here ....
   return Text("No match");
      }
        }

希望有帮助:)

我已经创建了一个演示。根据您的要求进行更改

import 'package:flutter/material.dart';

void main() =>
  runApp(MaterialApp(home: GridViewDemo()));

class GridViewDemo extends StatefulWidget {
  @override
  _GridViewDemoState createState() => _GridViewDemoState();
}

class _GridViewDemoState extends State<GridViewDemo> {
  // already added indices numbers
  List<int> alreadyAddedIndices = [3,4,5,6,7];

  var courseid = 0;
  var coursename = "default";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text(("GridView Demo")),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [
            GridView.builder(
              itemCount: 5,
              shrinkWrap: true,
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: (MediaQuery.of(context).orientation == Orientation.portrait) ? 3 : 4),
              itemBuilder: (BuildContext context, int index) {
                return Card(
                  child:InkWell(
                    onTap: () {
                      setState(() {
                        // change as per your code
                        courseid = index;
                        coursename=index.toString();
                        // add index in list if not available
                        // tapping again, remove index from list
                        alreadyAddedIndices.contains(index)?alreadyAddedIndices.remove(index):alreadyAddedIndices.add(index);
                      });
                    },
                    child:Column(
                      children: <Widget>[
                        Text((alreadyAddedIndices.contains(index)) ? "Added" : ""),
                        Icon(index.isEven ? Icons.school : Icons.book,
                          size:MediaQuery.of(context).orientation == Orientation.portrait ?30 : 30,
                          color: index.isOdd  ? Colors.amber[800]: Colors.green[800],),
                        // course name text
                        const Text("course Name"),
                      ],
                    ),
                  ),

                );
              },
            ),
          ],
        ),
      ),
    );
  }
}
导入“包装:颤振/材料.省道”;
void main()=>
runApp(MaterialApp(主页:GridViewDemo());
类GridViewDemo扩展了StatefulWidget{
@凌驾
_GridViewDemoState createState()=>\u GridViewDemoState();
}
类_GridViewDemoState扩展状态{
//已添加索引编号
列表已添加的数据=[3,4,5,6,7];
var-courseid=0;
var coursename=“默认”;
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:常量文本((“GridView演示”),
),
正文:SingleChildScrollView(
子:列(
儿童:[
GridView.builder(
物品计数:5,
收缩膜:对,
gridDelegate:SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount:(MediaQuery.of(context.orientation==orientation.grait)?3:4),
itemBuilder:(构建上下文,int索引){
回程卡(
孩子:InkWell(
onTap:(){
设置状态(){
//根据您的代码进行更改
courseid=索引;
coursename=index.toString();
//在列表中添加索引(如果不可用)
//再次点击,从列表中删除索引
包含(索引)?删除(索引):添加(索引);
});
},
子:列(
儿童:[
文本((alreadyAddedIndices.contains(index))?“添加”:“”),
Icon(index.isEven?Icons.school:Icons.book、,
大小:MediaQuery.of(context.orientation==orientation.grait?30:30,
颜色:index.isOdd?Colors.amber[800]:Colors.green[800],),
//课程名称文本
常量文本(“课程名称”),
],
),
),
);
},
),
],
),
),
);
}
}
输出:

注意:
这是一个演示代码。您可以在alreadyAddedIndices列表中获得所有添加的课程ID。根据需要更改代码。

您需要在一个列表上循环,并检查另一个列表中是否存在相同的id。当前在“子对象”列中,您只有一个对象。一旦你在这个列表上循环并返回这些小部件的列表,你将有多个孩子。如何在这里创建一个循环请帮助memeans点击课程卡,你想不想添加文本。对吗?在学生表中添加的点击卡课程如果学生已经添加了课程,则会显示一条添加到课程上的消息。谢谢您的回答,但问题是我从firebase云存储中获得了一些id,现在将此列表与主页上的可用课程id进行比较,我需要一个循环来比较,或者其他我现在不需要的方式。请帮帮我。
_ComparingLists(int id) {
bool temp = false;
  for (int i = 0; i < coursedata['courseid'].length; i++) {
  if ((coursedata['courseid'][i] == id)) {
  temp = true;
  break;
} else {
  temp = false;
}
 }

 // student is already enrolled
 if (temp == true) {
   return Text("Student is enrolled ...");
  }
  // student is not enrolled
  else {
  // do your operations like adding to the list here ....
   return Text("No match");
      }
        }
_ComparingLists(course[index]['id'])
import 'package:flutter/material.dart';

void main() =>
  runApp(MaterialApp(home: GridViewDemo()));

class GridViewDemo extends StatefulWidget {
  @override
  _GridViewDemoState createState() => _GridViewDemoState();
}

class _GridViewDemoState extends State<GridViewDemo> {
  // already added indices numbers
  List<int> alreadyAddedIndices = [3,4,5,6,7];

  var courseid = 0;
  var coursename = "default";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text(("GridView Demo")),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [
            GridView.builder(
              itemCount: 5,
              shrinkWrap: true,
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: (MediaQuery.of(context).orientation == Orientation.portrait) ? 3 : 4),
              itemBuilder: (BuildContext context, int index) {
                return Card(
                  child:InkWell(
                    onTap: () {
                      setState(() {
                        // change as per your code
                        courseid = index;
                        coursename=index.toString();
                        // add index in list if not available
                        // tapping again, remove index from list
                        alreadyAddedIndices.contains(index)?alreadyAddedIndices.remove(index):alreadyAddedIndices.add(index);
                      });
                    },
                    child:Column(
                      children: <Widget>[
                        Text((alreadyAddedIndices.contains(index)) ? "Added" : ""),
                        Icon(index.isEven ? Icons.school : Icons.book,
                          size:MediaQuery.of(context).orientation == Orientation.portrait ?30 : 30,
                          color: index.isOdd  ? Colors.amber[800]: Colors.green[800],),
                        // course name text
                        const Text("course Name"),
                      ],
                    ),
                  ),

                );
              },
            ),
          ],
        ),
      ),
    );
  }
}