首页 > 解决方案 > 试图从世界时间 api 颤振发出 json 请求

问题描述

当我尝试从包含 JSON 数据的资产文件夹中获取数据时,我正在尝试使用未来的构建器从世界时间 API 发出 JSON 请求,但当我尝试从 Internet 获取数据时,它崩溃了

在这里,您可以看到这是主要课程

    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),
                  ),
                );
              }
            }));
  }
}

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

    ════════ 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)
...

标签: jsonflutterandroid-studiodart

解决方案


你的钥匙不工作,用邮递员检查,你必须await得到回应

在此处输入图像描述


推荐阅读