首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试使来自worldtime api的json请求抖动

尝试使来自worldtime api的json请求抖动
EN

Stack Overflow用户
提问于 2021-04-18 05:26:16
回答 1查看 75关注 0票数 0

当我尝试从包含JSON数据的资产文件夹中获取数据时,我尝试使用future builder从world time API发出JSON请求,但当我尝试从互联网获取数据时,它崩溃了。

在这里,您可以看到主类

代码语言:javascript
复制
    import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:http/http.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.green,
        body: FutureBuilder(
            future:
                get('http://api.worldweatheronline.com/premium/v1/weather.ashx?key=65dbd1979bd445e58aa171529203010&q=Europe/London&format=json&num_of_days=1'),
            builder: (context, snapshot) {
              var myData = json.decode(snapshot.data.toString());
              String jsonsDataString = myData.body.toString(); // toString of Response's body is assigned to jsonDataString
              jsonsDataString = jsonDecode(jsonsDataString);
              if (myData == null){
                return Center(
                  child: Text(
                    'Loading',
                    style: TextStyle(fontSize: 30, color: Colors.red),
                  ),
                );
              }else{
                return Center(
                  child: Text(
                    myData,
                    style: TextStyle(fontSize: 30, color: Colors.red),
                  ),
                );
              }
            }));
  }
}

这是我尝试运行应用程序时出现的错误

代码语言:javascript
复制
    ════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following FormatException was thrown building FutureBuilder<Response>(dirty, state: _FutureBuilderState<Response>#2a0b7):
Unexpected character (at character 1)
Instance of 'Response'
^

The relevant error-causing widget was: 
  FutureBuilder<Response> file:///F:/FlutterProjects/learn_json/lib/main.dart:54:15
When the exception was thrown, this was the stack: 
#0      _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1394:5)
#1      _ChunkedJsonParser.parseNumber (dart:convert-patch/convert_patch.dart:1261:9)
#2      _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:926:22)
#3      _parseJson (dart:convert-patch/convert_patch.dart:31:10)
#4      JsonDecoder.convert (dart:convert/json.dart:495:36)
...
EN

回答 1

Stack Overflow用户

发布于 2021-04-18 06:16:41

您的密钥不起作用,请使用Postman进行检查,然后您必须使用await进行响应

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67143098

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档