Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 如何修复DateFormat()或DateTime()错误?_Flutter_Dart - Fatal编程技术网

Flutter 如何修复DateFormat()或DateTime()错误?

Flutter 如何修复DateFormat()或DateTime()错误?,flutter,dart,Flutter,Dart,下午好,我最近开始学习颤振。因为一个问题而停止。当我试图获取一周中的某一天时,我遇到了一个字符串错误,我不知道如何修复它 在这里我发布了代码的主要部分,您可以在GitHub上看到整个项目=> import'包:geologitor/geologitor.dart'; 进口“包装:颤振/材料.省道”; 导入“包:intl/intl.dart”; 导入“../helpers/weather.dart”; 导入“../components/searchForm.dart”; 导入“../compone

下午好,我最近开始学习颤振。因为一个问题而停止。当我试图获取一周中的某一天时,我遇到了一个字符串错误,我不知道如何修复它

在这里我发布了代码的主要部分,您可以在GitHub上看到整个项目=>

import'包:geologitor/geologitor.dart';
进口“包装:颤振/材料.省道”;
导入“包:intl/intl.dart”;
导入“../helpers/weather.dart”;
导入“../components/searchForm.dart”;
导入“../components/weatherCard.dart”;
导入“../components/big_weather_card.dart”;
导入“../components/additional_weather_info.dart”;
导入“../components/weather_bar.dart”;
类主页扩展了StatefulWidget{
@凌驾
State createState()=>HomePageState();
}
类HomePageState扩展了状态{
最终地理定位器_Geolocator=Geolocator()…forceAndroidLocationManager;
位置(u位置),;
弦乐城;
字符串图标;
字符串_desc;
字符串描述;
内压;
内部湿度;
内部温度;
天气预报表;
WeatherFetch _WeatherFetch=WeatherFetch();
最终布尔调试=真;
@凌驾
void initState(){
_city=“空”;
_温度=0;
_icon=“04n”;
_description=“empty”;
_压力=0;
_湿度=0;
super.initState();
_getCurrent();
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
resizeToAvoidBottomInset:false,
appBar:appBar(
标题:正文(
“天气预报”,
样式:Theme.of(context.textTheme.bodyText1.copyWith(fontSize:24),
),
标题:对,
),
正文:调试
?立柱(
儿童:[
BigWeatherCard(
iconCode:_图标,
温度:_温度,
韦瑟德斯:描述,
),
附加天气信息(
压力:_压力,
湿度:_湿度,
),
天气栏(天气图:_天气列表),
],
)
:列(
儿童:[
搜索(_changeCity),
居中(
子:文本(
“$_市”,
样式:TextStyle(fontSize:30,fontWeight:fontWeight.bold),
),
),
天气卡(标题:描述,温度:温度,图标代码:图标)
],
),
);
}
_getCurrent(){
_地理定位器
.getCurrentPosition(所需精度:定位精度。最佳)
.然后((位置){
设置状态(){
_位置=位置;
});
_getCityAndWeather();
_获取未来天气();
});
}
_getCityAndWeather()异步{
列表p=等待_geologitor.placemarkFromCoordinates(
_位置。纬度,_位置。经度);
Placemark place=p[0];
var data=await\u weatherFetch.getWeatherByCoord(
_位置。纬度,_位置。经度);
更新当前天气数据(数据);
设置状态(){
_城市=地点。地点;
});
}
_getFutureWeather()异步{
var data=await\u weatherFetch.getWeatherByCoordAllInOne(
_位置。纬度,_位置。经度);
更新未来天气数据(数据);
setState((){});
}
_changeCity(城市)异步{
试一试{
var data=wait_weatherFetch.getWeatherByName(城市);
更新当前天气数据(数据);
设置状态(){
_城市=城市;
});
}捕获(ex){
印刷品(ex);
}
}
void updateCurrentWeatherData(weatherData){
设置状态(){
如果(weatherData!=null){
_temp=weatherData['main']['temp'].toInt();
_icon=weatherData['weather'][0]['icon'];
_desc=weatherData['main']['感觉像'].toString();
_description=weatherData['weather'][0]['description'].toString();
_压力=天气数据['main']['pressure'].toInt();
_湿度=天气数据['main']['湿度'].toInt();
}否则{
_温度=0;
_city=“不知道在哪里”;
_icon=“04n”;
}
});
}
void updateFutureWeatherData(weatherData){
如果(weatherData==null){
打印('weatherData==null');
返回;
}
设置状态(){
_weatherList=List.generate(
5.
(一){
返回{
“day”:DateFormat.E()
.format(DateTime(weatherData['daily'][i]['dt'].toInt()),
“图标”:weatherData['daily'][i]['weather'][0]['icon'].toString(),
“临时”:weatherData['daily'][i]['temp']['day'].toString(),
};
},
);
});
}
}
调试控制台

E/flutter (27586): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: Invalid argument(s)
E/flutter (27586): #0      new DateTime._internal (dart:core-patch/date_patch.dart:54:23)
E/flutter (27586): #1      new DateTime (dart:core/date_time.dart:180:14)
E/flutter (27586): #2      HomePageState.updateFutureWeatherData.<anonymous closure>.<anonymous closure>
package:workshop_weathercast/components/homePage.dart:176
E/flutter (27586): #3      new _GrowableList.generate (dart:core-patch/growable_array.dart:133:28)
E/flutter (27586): #4      HomePageState.updateFutureWeatherData.<anonymous closure>
package:workshop_weathercast/components/homePage.dart:171
E/flutter (27586): #5      State.setState
package:flutter/…/widgets/framework.dart:1089
E/flutter (27586): #6      HomePageState.updateFutureWeatherData
package:workshop_weathercast/components/homePage.dart:170
E/flutter (27586): #7      HomePageState._getFutureWeather
package:workshop_weathercast/components/homePage.dart:121
E/flutter (27586): <asynchronous suspension>
E/flutter (27586):
E/flatter(27586):[错误:flatter/lib/ui/ui\u dart\u state.cc(186)]未处理的异常:无效参数
E/颤振(27586):#0新日期时间。#内部(省道:核心补丁/日期补丁。省道:54:23)
E/Flatter(27586):#1个新的日期时间(dart:core/date_time。dart:180:14)
E/Flatter(27586):#2主页状态。更新未来天气数据。。
包装:workshop_weathercast/组件/主页。dart:176
E/flatter(27586):#3个新的GrowtableList.generate(dart:core patch/Growtable_array.dart:133:28)
E/颤振(27586):#4 HomePageState.updateFutureWeatherData。
包装:workshop_weathercast/组件/主页。dart:171
E/颤振(27586):#5 State.setState
包:flatter/../widgets/framework.dart:1089
E/颤振(27586):#6 HomePageState.updateFutureWeatherData
包装:workshop_weathercast/组件/主页。dart:170
E/Flatter(27586):#7主页状态。"获取未来天气
包装:workshop_weathercast/组件/主页。dart:121
E/颤振(27586):
E/颤振(27586):

看起来
dt
是时间戳,您应该使用
fromsmillissecondssinceepoch

DateTime.fromMillisecondsSinceEpoch(weatherData['daily'][i]['dt'].toInt() * 1000)
DateTime.fromMillisecondsSinceEpoch(weatherData['daily'][i]['dt'].toInt() * 1000)