如何在 flutter 中使用 where 来操作提供者的列表?

How to use where in manipulating the list from a provider in flutter?

您好,我想弄清楚这个问题已经很久了,但仍然不确定如何根据 selected/passed 类别显示项目...

  1. 我只需要显示所选类别下的项目

类别注释屏幕:

我尝试打印但没有显示任何不确定如何使用和使用什么的内容。目前所有笔记都在显示,即使我有一个用于过滤它们的类别 ID。

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../providers/notes.dart';
import '../providers/categories.dart';
import '../widgets/category_note_item.dart';
class CategoryNotesScreen extends StatelessWidget {
  static const routeName = '/category-notes';

  @override
  Widget build(BuildContext context) {
    Category routeArgs = ModalRoute.of(context).settings.arguments;
    final catId = routeArgs.id;
    final catTitle = routeArgs.title;
    var notesData = Provider.of<Notes>(context);
    print(notesData.items.where((item) => item.category.contains(catId)));
    return Scaffold(
      appBar: AppBar(
        title: Text(catTitle),
      ),
      body: ListView.builder(
        itemCount: notesData.items.length,
        itemBuilder: (ctx, i) => CategoryNoteItem(notesData.items[i]),
      ),
    );
  }
}

类别注释项目小部件:

import 'package:flutter/material.dart';

import '../providers/notes.dart' as nte;

class CategoryNoteItem extends StatelessWidget {
  final nte.NoteItem note;
  CategoryNoteItem(this.note);

  @override
  Widget build(BuildContext context) {
    return Card(
      margin: EdgeInsets.all(10),
      child: Column(
        children: <Widget>[
          ListTile(
            title: Text('${note.question}'),
            subtitle: Text('${note.answer}'),
            // trailing: ,
          )
        ],
      ),
    );
  }
}

类别提供商:

import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';

class Category with ChangeNotifier {
  final String id;
  final String title;

  Category({
    @required this.id,
    @required this.title,
  });
}
class Categories with ChangeNotifier {
  List<Category> _items = [
    Category(
      id: 'c1',
      title: 'Default',
    ),
    Category(
      id: 'c2',
      title: 'French Lesson',
    ),
    Category(
      id: 'c3',
      title: 'Spanish Lesson',
    ),
  ];
  List<Category> get items {
    return [..._items];
  }}

笔记提供者:

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

class NoteItem with ChangeNotifier {
  final String id;
  final String category;
  final String question;
  final String answer;

  NoteItem({
    @required this.id,
    @required this.category,
    @required this.question,
    @required this.answer,
  });

}
class Notes with ChangeNotifier {
  List<NoteItem> _items = [
    NoteItem(
      id: 'n1',
      category: 'c1',
      question: 'q1',
      answer: 'a1',
    ),
    NoteItem(
      id: 'n2',
      category: 'c1',
      question: 'q2',
      answer: 'a2',
    ),
    NoteItem(
      id: 'n3',
      category: 'c2',
      question: 'q3',
      answer: 'a3',
    ),
    NoteItem(
      id: 'n4',
      category: 'c1',
      question: 'q4',
      answer: 'a4',
    ),
  ];
  
  List<NoteItem> get items {
    return [..._items];
  }
}

抱歉,我是 flutter 的新手,仍在学习中...

经过反复试验,我发现我可以使用它来过滤来自提供商的列表:

notesData.items.where((item) => item.category.contains(catId)).toList();

并在 itembuilder 中添加:

itemBuilder: (ctx, i) =>  CategoryNoteItem(
              notes[i].id,
              notes[i].category,
              notes[i].question,
              notes[i].answer,
            ),

我只能显示所选类别中的项目。