Project doesn't compile when using Intl package (DateFormat)

牧云@^-^@ 提交于 2021-02-07 22:35:09

问题


I'm trying to run my app after importing intl, but it won't work.

In the pubspec.yaml file I have:

intl: ^0.15.8

I've imported the package in three ways after some research, just to be sure. But only the first one (intl.dart) seems to be used :

import "package:intl/intl.dart";
import "package:intl/intl_browser.dart"; //unused import
import "package:intl/intl_standalone.dart"; //unused import

I'm using it this way :

DateFormat("dd-MM-yyyy").format(_chosenDate)

My error after flutter run is:

Resolving dependencies...                                           2,3s
                                                                        
Compiler message:                                                       
file:///Users/mydomus/Documents/flutter/.pub-cache/hosted/pub.dartlang.org/intl-0.15.8/lib/intl_browser.dart:25:48: Error: Getter not found: 'window'.
  Intl.systemLocale = Intl.canonicalizedLocale(window.navigator.language);
                                               ^^^^^^                   
file:///Users/mydomus/Documents/flutter/.pub-cache/hosted/pub.dartlang.org/intl-0.15.8/lib/intl_browser.dart:13:8: Error: Not found: 'dart:html'
import "dart:html";                                                     
       ^                                                                
Unhandled exception:                                                    
FileSystemException(uri=org-dartlang-untranslatable-uri:dart%3Ahtml; message=StandardFileSystem only supports file:* and data:* URIs)
#0      StandardFileSystem.entityForUri (package:front_end/src/api_prototype/standard_file_system.dart:33:7)
#1      asFileUri (package:vm/kernel_front_end.dart:589:37)             
<asynchronous suspension>                                               
#2      FrontendCompiler._outputDependenciesDelta (package:vm/frontend_server.dart:391:39)
<asynchronous suspension>                                               
#3      FrontendCompiler.compile (package:vm/frontend_server.dart:365:13)
<asynchronous suspension>                                               
#4      _FlutterFrontendCompiler.compile (package:frontend_server/server.dart:31:22)
<asynchronous suspension>                                               
#5      starter (package:frontend_server/server.dart:133:27)            
<asynchronous suspension>                                               
#6      main (file:///b/s/w/ir/k/src/flutter/frontend_server/bin/starter.dart:8:30)
<asynchronous suspension>                                               
#7      _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:298:32)
#8      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:171:12)
                                                                        
Compiler failed on /Users/mydomus/Desktop/meriem/flutter/mydomus_edl/lib/main.dart

Am I doing something wrong? or is it a package issue that I should report?


回答1:


I solved it by changing the import. Instead of importing:

import "package:intl/intl_browser.dart";

import the following:

import 'package:intl/intl_standalone.dart';



回答2:


This is because you have an import of "dart:html", but it doesn't exist on the platform you are deploying to.

See this line in the exception you posted:

import "dart:html"; 

This is a similar issue:

https://stackoverflow.com/a/59310849/3268303




回答3:


The import from the intl package of import "package:intl/intl_browser.dart";, tries using the dart:html package which is not configured.


Use the following import instead:

import 'package:intl/intl_standalone.dart';



回答4:


I was also getting this error. Just run the command flutter clean. After running this command the issue was solved.



来源:https://stackoverflow.com/questions/56575071/project-doesnt-compile-when-using-intl-package-dateformat

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!