본문 바로가기

개발하자

'DateTime' 유형이 Flower Cloud Firestore의 'String' 유형의 하위 유형이 아닙니다.

반응형

'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

()


반응형