Upgrade Skill Di ITBOX Jadi Lebih Hemat
Special Diskon 50%
Sampai 16 November 2026!
Hari
Jam
Menit
Detik
GARUDABOX

_notes[index].updatedAt error di providers/notes.dart

Beranda › Forums › Belajar Flutter Mobile Apps Tingkat Menengah › _notes[index].updatedAt error di providers/notes.dart

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #187396
    Erick Darmawan
    Participant

      coach sy mau tanya kenapa _notes[index].updatedAt error di void toogleIsPinned(String id, kata isi erronya = ‘The argument type ‘DateTime?’ can’t be assigned to the parameter type ‘DateTime”?

      ini isi kode id providers/notes.dart:

      import ‘dart:io’;

      import ‘package:flutter/material.dart’;
      import ‘package:notes/api/note_api.dart’;
      import ‘package:notes/models/note.dart’;

      class Notes with ChangeNotifier {
      List<Note> _notes = [
      Note(
      id: ‘N1’,
      title: ‘Catatan Materi Flutter’,
      note:
      ‘Flutter merupakan Software Development Kit (SDK) yang bisa membantu developer dalam membuat aplikasi mobile cross platform. Kelas ini akan mempelajari pengembangan aplikasi mobile yang dapat dijalankan baik di IOS maupun di Android’,
      updatedAt: DateTime.parse(‘2021-05-19 20:33:33’),
      createdAt: DateTime.parse(‘2021-05-19 20:33:33’),
      ),
      Note(
      id: ‘N2’,
      title: ‘Target Pembelajaran Flutter’,
      note:
      ‘Peserta dapat mengembangkan aplikasi mobile (IOS dan Android) menggunakan flutter,\nPeserta memahami konsep pengembangan aplikasi menggunakan flutter,\nPeserta dapat menjalankan aplikasi mobile di IOS dan Android ataupun Emulator,\nPeserta memahami bahasa pemrograman Dart,\nPeserta dapat mendevelop aplikasi mobile menggunakan flutter dan dart dari dasar secara berurutan.’,
      updatedAt: DateTime.parse(‘2021-05-20 20:53:33’),
      createdAt: DateTime.parse(‘2021-05-20 20:53:33’),
      ),
      Note(
      id: ‘N3’,
      title: ‘Belajar Flutter di ITBOX’,
      note: ‘Jangan lupa belajar flutter dengan video interactive di ITBOX.’,
      updatedAt: DateTime.parse(‘2021-05-20 21:22:33’),
      createdAt: DateTime.parse(‘2021-05-20 21:22:33’),
      ),
      Note(
      id: ‘N4’,
      title: ‘Resep nasi goreng’,
      note:
      ‘Nasi putih 1 piring\nBawang putih 2 siung, cincang halus\nKecap manis atau kecap asin sesuai selera\nSaus sambal sesuai selera\nSaus tiram sesuai selera\nGaram secukupnya\nKaldu bubuk rasa ayam atau sapi sesuai selera\nDaun bawang 1 batang, cincang halus\nTelur ayam 1 butir\nSosis ayam 1 buah, iris tipis\nMargarin atau minyak goreng 3 sdm.’,
      updatedAt: DateTime.parse(‘2021-05-20 21:51:33’),
      createdAt: DateTime.parse(‘2021-05-20 21:51:33’),
      )
      ];

      Future<void> getAndSetNotes() async {
      try {
      _notes = await NoteApi().getAllNote();
      } on SocketException {
      notifyListeners();
      return;
      } catch (e) {
      return Future.error(e);
      }
      notifyListeners();
      }

      List<Note> get notes {
      List<Note> tempListNote = _notes.where((note) => note.isPinned).toList();
      tempListNote.addAll(_notes.where((note) => !note.isPinned));
      return tempListNote;
      }

      void toggleIsPinned(String id) {
      int index = _notes.indexWhere((note) => note.id == id);
      if (index >= 0) {
      _notes[index].isPinned = !_notes[index].isPinned;
      _notes[index] = _notes[index].copyWith(updatedAt: DateTime.now());
      NoteApi()
      .toggleIsPinned(id, _notes[index].isPinned, //_notes[index].updatedAt <= disini errornya);
      notifyListeners();
      }
      }

      Future<void> addNote(Note note) async {
      String? id = await NoteApi().postNote(note);
      note = note.copyWith(id: id);
      _notes.add(note);
      notifyListeners();
      }

      Note getNote(String id) {
      return _notes.firstWhere((note) => note.id == id);
      }

      Future<void> updateNote(Note newNote) async {
      await NoteApi().updateNote(newNote);
      int index = _notes.indexWhere((note) => note.id == newNote.id);
      _notes[index] = newNote;
      notifyListeners();
      }

      void deleteNote(String id) {
      _notes.removeWhere((note) => note.id == id);
      notifyListeners();
      }
      }

      dan ini model dari models/note.dart:

      class Note {
      final String id;
      final String title;
      final String note;
      final DateTime? updatedAt;
      final DateTime? createdAt;
      bool isPinned;

      Note(
      {required this.id,
      required this.title,
      required this.note,
      required this.updatedAt,
      required this.createdAt,
      this.isPinned = false});

      Note copyWith({
      String? id,
      String? title,
      String? note,
      DateTime? updatedAt,
      DateTime? createdAt,
      bool? isPinned,
      }) {
      return Note(
      id: id ?? this.id,
      title: title ?? this.title,
      note: note ?? this.note,
      updatedAt: updatedAt ?? this.updatedAt,
      createdAt: createdAt ?? this.createdAt,
      isPinned: isPinned ?? this.isPinned,
      );
      }
      }

      cara mengatasi masalah _notes[index].updatedAt ini gimana coach?

      #187697
      Erick Darmawan
      Participant

        btw, saya versi flutter sy 3.6.0-13.0.pre

        #191001
        Anonymous

          Halo, maaf baru bales
          itu sesuai pesan errornya ‘The argument type ‘DateTime?’ can’t be assigned to the parameter type ‘DateTime’
          kamu memberikan value updatedAt yang kemungkinan valuenya bisa null sedangkan function toggleIsPinned punya kamu nerimanya variable DateTime tanpa ? yang tidak bisa null.

          untuk versi dart sdk kamu yang di pubspec.yaml bisa diturunin dulu ya sesuaiin dengan yang divideo pas materi basic.
          nanti pas sudah sampai materi advanced flutter akan diajarin terkait materi null safety yang berkaitan dengan error kamu biar kamu bisa lebih jelas.

        Viewing 3 posts - 1 through 3 (of 3 total)
        • You must be logged in to reply to this topic.
        Shopping cart0
        There are no products in the cart!
        Continue shopping