반응형
'DateTime' 유형이 Flower Cloud Firestore의 'String' 유형의 하위 유형이 아닙니다.
DateTime.now()를 가져와서 Cloud FireStore로 쓰고 읽으려고 합니다. 하지만 오류가 발생했다.
이게 내 코드야.
UI에서
Text(
DateFormat('dd/MM/yyyy kk:mm')
.format(DateTime
.fromMillisecondsSinceEpoch(int.parse(document[index].data['timeCreated']))),
style: timeStyle,
),
Firestore에 데이터 쓰기
Firestore.instance.runTransaction((transaction) async {
await transaction.set(
docRef,
{'timeCreated': DateTime.now().millisecondsSinceEpoch.toString()},
);
});
그리고 이 오류를 받았다.
I/flutter ( 3378): Another exception was thrown: type 'DateTime' is not a subtype of type 'String'
문자열로 변환:
int time = DateTime.now().millisecondsSinceEpoch;
String t = "$time";
여기서 유형이 'String'으로 예상되는데, 잘못된 'DateTime'으로 전달하고 있습니다.
기본 'en_'의 날짜 형식 지정US' 형식은 초기화가 필요하지 않습니다.
https://api.flutter.dev/flutter/intl/DateFormat-class.html
따라서 다음과 같이 포맷하기만 하면 됩니다.
var dateTime = DateTime.now()
DateFormat.E().format(dateTime)
E()는 날짜 형식의 생성자로, 주의 첫 번째 3개의 문자를 참조합니다.
공식 문서에서 사용 가능한 생성자를 참조할 수 있습니다.
https://api.flutter.dev/flutter/intl/DateFormat-class.html#생성자
: 아래는 DateTime 라이브러리에 사용되는 버전입니다. intl: ^0.18.0
()
반응형
'개발하자' 카테고리의 다른 글
Flurter 및 Android Studio를 설치한 후 "cmdline-tools 구성 요소가 누락됨" 오류가 표시됩니다. Android SDK를 추가했습니다. 어떻게 해결해야 할까요? (0) | 2022.12.17 |
---|---|
Kubernetes 조타 차트를 설치할 수 없습니다. 오류: 아직 사용 중인 이름을 재사용할 수 없습니다. (0) | 2022.12.17 |
fastapi/flask/nodejs의 쿼리 매개 변수를 따르는 대괄호 (1) | 2022.12.16 |
다트 코드로만 플러터 웹 API 오류를 해결하는 방법은 무엇입니까? (0) | 2022.12.15 |
주피터 서버: 시작되지 않음, vs 코드에 커널 없음 (0) | 2022.12.14 |