Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.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 在空颤振中调用了future方法_Flutter_Dart_Future - Fatal编程技术网

Flutter 在空颤振中调用了future方法

Flutter 在空颤振中调用了future方法,flutter,dart,future,Flutter,Dart,Future,我正在获取数据api,并将其从Json转换。 我有这个方法来获取数据,打赌当在未来的构建器中使用它时,它会给我 getPData()方法在null上被调用。 这是我创建getPData方法的地方: class JsonConnection { JsonConnection.jsonDecode(); static double pLat; static double pLong; PData timesList; Future getPData() async {

我正在获取数据api,并将其从Json转换。 我有这个方法来获取数据,打赌当在未来的构建器中使用它时,它会给我 getPData()方法在null上被调用。

这是我创建getPData方法的地方:

class JsonConnection {
  JsonConnection.jsonDecode();

  static double pLat;
  static double pLong;    
PData timesList;
  Future getPData() async {
    final pos = await Geolocator.getCurrentPosition(
        desiredAccuracy: LocationAccuracy.medium);

    pLat = pos.latitude;
    pLong = pos.longitude;
    String date = DateTime.now().toString();
    int method = 4;
    final url = Uri.parse(
        'http://api.aladhan.com/v1/timings/$date?latitude=$pLat&longitude=$pLong&method=$method');
    http.Response res = await http.get(url);
    var data = json.decode(res.body);
    timesList = PData.fromJson(data);
    return timesList;
  }
 body: FutureBuilder(
      future: jsonConnection.getPData(),
      builder: (BuildContext context, AsyncSnapshot snapshot) {
        print("hello");
        print(snapshot.data.meta.timezone);
        if (snapshot.hasData) {
          CircleAvatar();
          return Center(
              child: Container(
                  padding: const EdgeInsets.only(top: 150.0),
                  child: Column(
                    children: [
                      Table(
                        border: TableBorder.all(color: Colors.purpleAccent),
                        defaultColumnWidth: FixedColumnWidth(150.0),
                        defaultVerticalAlignment:
                            TableCellVerticalAlignment.middle,
                        children: <TableRow>[
                          TableRow(
                            children: <Widget>[
                              TableCell(
                                  verticalAlignment:
                                      TableCellVerticalAlignment.middle,
                                  child: Center(
                                    child: Text(snapshot.data.meta.timezone,
                                        style: TextStyle(
                                            color: Colors.black,
                                            fontSize: 18,
                                            fontWeight: FontWeight.bold)),
                                  )),
                         
这里是我使用getPData的地方:

class JsonConnection {
  JsonConnection.jsonDecode();

  static double pLat;
  static double pLong;    
PData timesList;
  Future getPData() async {
    final pos = await Geolocator.getCurrentPosition(
        desiredAccuracy: LocationAccuracy.medium);

    pLat = pos.latitude;
    pLong = pos.longitude;
    String date = DateTime.now().toString();
    int method = 4;
    final url = Uri.parse(
        'http://api.aladhan.com/v1/timings/$date?latitude=$pLat&longitude=$pLong&method=$method');
    http.Response res = await http.get(url);
    var data = json.decode(res.body);
    timesList = PData.fromJson(data);
    return timesList;
  }
 body: FutureBuilder(
      future: jsonConnection.getPData(),
      builder: (BuildContext context, AsyncSnapshot snapshot) {
        print("hello");
        print(snapshot.data.meta.timezone);
        if (snapshot.hasData) {
          CircleAvatar();
          return Center(
              child: Container(
                  padding: const EdgeInsets.only(top: 150.0),
                  child: Column(
                    children: [
                      Table(
                        border: TableBorder.all(color: Colors.purpleAccent),
                        defaultColumnWidth: FixedColumnWidth(150.0),
                        defaultVerticalAlignment:
                            TableCellVerticalAlignment.middle,
                        children: <TableRow>[
                          TableRow(
                            children: <Widget>[
                              TableCell(
                                  verticalAlignment:
                                      TableCellVerticalAlignment.middle,
                                  child: Center(
                                    child: Text(snapshot.data.meta.timezone,
                                        style: TextStyle(
                                            color: Colors.black,
                                            fontSize: 18,
                                            fontWeight: FontWeight.bold)),
                                  )),
                         
body:FutureBuilder(
future:jsonConnection.getPData(),
生成器:(BuildContext上下文,异步快照){
打印(“你好”);
打印(snapshot.data.meta.timezone);
if(snapshot.hasData){
圆形的();
返回中心(
子:容器(
填充:仅限常量边集(顶部:150.0),
子:列(
儿童:[
桌子(
边框:TableBorder.all(颜色:Colors.purpleAccent),
defaultColumnWidth:FixedColumnWidth(150.0),
默认垂直对齐:
TableCell垂直对齐。中间,
儿童:[
桌椅(
儿童:[
表细胞(
垂直排列:
TableCell垂直对齐。中间,
儿童:中心(
子项:文本(snapshot.data.meta.timezone,
样式:TextStyle(
颜色:颜色,黑色,
尺码:18,
fontWeight:fontWeight.bold),
)),

我能做些什么来修复它?

您是否在终端中测试了您的
url
?您是否检查了
PData。fromJson
工作正常?错误消息清楚地说明了发生了什么

我已经用这段代码验证了API,它正在工作:

对于Dart: 您可以用典型的
http.get
替换
HttpReqService.getJson

对于颤振:
导入“包装:颤振/材料.省道”;
导入“包:wnetworking/wnetworking.dart”;
班级游戏时间{
静态常数https://api.aladhan.com/v1';
静态未来获取计时({DateTime?date,双纬度,双经度,int method=2,String?tune,int school=0,int midnight mode=0,String?timezonestring,int?latitudeAdjustmentMethod,int?adjustment})异步{
String eval(String parName,var parValue)=>parValue!=null?'&$parName=$parValue':'';
var url='$_url/timings/${date??DateTime.now().millisondssinceepoch}?method=$method&school=$school&middnightmode=$middnightmode${eval(‘纬度’、纬度)}${eval(‘经度’、经度)}${eval(‘调谐’、调谐)}${eval(‘时区线’、时区线)}${eval(‘纬度调整’、纬度调整)};
返回等待HttpReqService
.getJson(url)
.然后((回应){
if(response!=null)返回响应['data']作为映射;
});
}
}
类主页扩展了无状态小部件{
/* ---------------------------------------------------------------------------- */
const主页({Key?Key}):超级(Key:Key);
/* ---------------------------------------------------------------------------- */
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本('Hi!'),
标题:对,
),
主体:容器(
孩子:未来建设者(
未来:播放时间。获取时间(纬度:51.508515,经度:0.1254872),
生成器:(上下文,快照){
if(snapshot.connectionState==connectionState.done){
if(snapshot.hasError)返回文本(snapshot.error.toString());
if(snapshot.hasData)返回文本(snapshot.data.toString());
返回文本('无数据…');
}
返回循环ProgressIndicator();
},
),
),
);
}
}
结果:

Fetching timing... done

{timings: {Fajr: 03:17, Sunrise: 05:20, Dhuhr: 12:56, Asr: 17:03, Sunset: 20:33, Maghrib: 20:33, Isha: 22:36, Imsak: 03:07, Midnight: 00:56}, date: {readable: 07 May 2021, timestamp: 1620342000, hijri: {date: 25-09-1442, format: DD-MM-YYYY, day: 25, weekday: {en: Al Juma'a, ar: الجمعة}, month: {number: 9, en: Ramaḍān, ar: رَمَضان}, year: 1442, designation: {abbreviated: AH, expanded: Anno Hegirae}, holidays: [Lailat-ul-Qadr]}, gregorian: {date: 07-05-2021, format: DD-MM-YYYY, day: 07, weekday: {en: Friday}, month: {number: 5, en: May}, year: 2021, designation: {abbreviated: AD, expanded: Anno Domini}}}, meta: {latitude: 51.508515, longitude: 0.1254872, timezone: Europe/London, method: {id: 2, name: Islamic Society of North America (ISNA), params: {Fajr: 15, Isha: 15}}, latitudeAdjustmentMethod: ANGLE_BASED, midnightMode: STANDARD, school: STANDARD, offset: {Imsak: 0, Fajr: 0, Sunrise: 0, Dhuhr: 0, Asr: 0, Maghrib: 0, Sunset: 0, Isha: 0, Midnight: 0}}}

Job done!

非常感谢,正如你所说,我遵循了一个教程,但我没有测试它,所以我迷路了!好吧,我还有更多的问题,我如何将结果放入文本小部件?我尝试如上所述使用snapshot,但它给了我错误“getter'timings'被调用为null。Receiver:null”我更新了我的响应