Beranda › Forums › Belajar Flutter Mobile Apps Tingkat Menengah › _notes[index].updatedAt error di providers/notes.dart
- This topic has 2 replies, 2 voices, and was last updated 3 years, 10 months ago by Anonymous.
-
AuthorPosts
-
December 5, 2022 at 1:54 PM #187396
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?
December 5, 2022 at 4:31 PM #187697btw, saya versi flutter sy 3.6.0-13.0.pre
December 30, 2022 at 10:54 AM #191001AnonymousHalo, 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. -
AuthorPosts
- You must be logged in to reply to this topic.