Notes date filtration from db

0 votes

I want to filter data from the database by date, but I can't do it. As a result, only the Note for the month I need should be output.

I tried to do filtering, but I get an error:

Class 'Note' has no instance method '[]'. 
Receiver: Instance of 'Note' 
Tried calling: []("noteDate")

NoteList class

final List _allNotes = [];
  List<Map<String, dynamic>> _foundNotes = [];


  @override
  initState() {
    for (var note in notesModel.entityList) { _allNotes.add(note); }
    print('_allNotes = $_allNotes');
    // at the beginning, all users are shown
    _foundNotes = _allNotes.cast<Map<String, dynamic>>();
    _runFilter(date.month);

    super.initState();
  }

  void _runFilter(date) {
    List<Map<String, dynamic>>? results = [];
    print(date);
    print(_allNotes);
    if (date == null) {
      results = _allNotes.cast<Map<String, dynamic>>();
    } else {
      results = _allNotes
          .where((note) => note['noteDate'].contains(date)).cast<Map<String, dynamic>>()
          .toList();
    }

    // Refresh the UI
    setState(() {
      _foundNotes = results!;
      print('## _runFilter setState() ${_foundNotes}');
    });
  }

Note class:

class Note {
/// The fields this entity type contains.
int? id;
String? title;
String? content;
String? mood;
String? noteDate; // YYYY,MM,DD
String? noteTime; // HH,MM

/// Just for debugging, so we get something useful in the console.
String toString() {
return "{id: $id, title: $title, content: $content, color: $mood, noteDate: $noteDate, noteDate: $noteTime}";
}
}

Also, I can't get the notes data right away when I run the code

Apr 18, 2023 in Flutter by Ashwini
• 5,430 points
644 views

1 answer to this question.

0 votes

The error message you're seeing is telling you that the [] operator is not defined for the Note class. This means you can't access the fields of the Note objects using the bracket notation note['noteDate']. Instead, you need to use dot notation to access the fields, like this: note.noteDate.

Here's an updated version of your _runFilter method that uses dot notation to access the noteDate field:

void _runFilter(int? month) {
  List<Map<String, dynamic>> results = [];

  if (month == null) {
    results = _allNotes.map((note) => note.toJson()).toList();
  } else {
    results = _allNotes
        .where((note) => note.noteDate != null && note.noteDate!.split(',')[1] == month.toString())
        .map((note) => note.toJson())
        .toList();
  }

  setState(() {
    _foundNotes = results;
  });
}

This code assumes that the noteDate field is a string in the format "YYYY,MM,DD". It splits the string by the comma separator and checks if the second component (month) matches the month argument.

Also, note that I removed the unnecessary null-aware operator ! in the setState call. Since _foundNotes is initialized to a non-null empty list, you don't need to use the null-aware operator there.

answered Apr 18, 2023 by Tej

Related Questions In Flutter

0 votes
1 answer

How to call a Dart async function from JavaScript

To call a Dart async function from ...READ MORE

answered Mar 31, 2023 in Flutter by Meghana
988 views
0 votes
1 answer

How to get barcode from PDA scanner device in flutter

It's possible that the data sent by ...READ MORE

answered Apr 3, 2023 in Flutter by seena
1,944 views
0 votes
1 answer
0 votes
1 answer

Dart_LoadScriptFromKernel: The binary program does not contain 'main'.

Hi@akhtar, You need to add the main() function ...READ MORE

answered Jul 21, 2020 in Others by MD
• 95,460 points
3,556 views
0 votes
1 answer

How to install Dart in Windows system?

Hi@akhtar, Dart is the programming language used to code Flutter apps. ...READ MORE

answered Jul 21, 2020 in Others by MD
• 95,460 points
1,110 views
0 votes
1 answer

flutter run: No connected devices

Hi@akhtar, To prepare to run and test your ...READ MORE

answered Jul 21, 2020 in Others by MD
• 95,460 points
4,117 views
0 votes
1 answer

How to create a function in Dart language?

Hi@akhtar, There are many function types available in ...READ MORE

answered Jul 22, 2020 in Others by MD
• 95,460 points
749 views
0 votes
1 answer

How to Deserialize a list of objects from json in flutter Ask Question?

To deserialize a list of objects from ...READ MORE

answered Mar 28, 2023 in Flutter by vishalini
2,282 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP