如何解决Uncaught LocaleDataException: Locale data has not been initialized, call initializeDateFormatting(<locale>)?

码云
2020-11-20 16:25

flutter中使用代码格式化日期的时候指定了locale为zh_CN结果报错:Uncaught LocaleDataException: Locale data has not been initialized, call initializeDateFormatting(<locale>)。

这个问题的解决方式如下:

 

  • 引入intl包:
dependencies:
  intl: ^0.16.0

 

  • 示例代码:
import 'package:intl/intl.dart';
import 'package:intl/date_symbol_data_local.dart';

void main() {
  //初始化Formatter
  initializeDateFormatting();
  //指定locale并格式化日期2020-11-20
  print(DateFormat('EEE',"zh_CN").format(DateTime.now()));
  //输出内容
  //周五
}

 

全部评论