在flutter中发出http.get()请求时出现问题

6 get http dart flutter

我正在学习 flutter 中的 API 和 http 请求,我在发出 get 请求时遇到问题,因为在任何教程中,他们都直接将字符串 URL 粘贴到 get 中作为参数,但当我将其作为字符串发布时,它显示错误:

参数类型“String”无法分配给参数类型“Uri”。

有人能帮我吗?这是我的示例代码:

import 'dart:convert' as convert;
import 'package:http/http.dart' as http;

void main(List<String> arguments) async {
  // This example uses the Google Books API to search for books about http.
  // https://developers.google.com/books/docs/overview
  var url = 'https://www.googleapis.com/books/v1/volumes?q={http}';

  // Await the http get response, then decode the json-formatted response.
  var response = await http.get(url); // i am getting error here
  if (response.statusCode == 200) {
    var jsonResponse = convert.jsonDecode(response.body);
    var itemCount = jsonResponse['totalItems'];
    print('Number of books about http: $itemCount.');
  } else {
    print('Request failed with status: ${response.statusCode}.');
  }
}
Run Code Online (Sandbox Code Playgroud)

这是我的代码的图像,有错误:

在此输入图像描述

小智 17

首先,将 http 库添加到您的项目中:

flutter pub add http
Run Code Online (Sandbox Code Playgroud)

然后将 http 导入为 http:

import 'package:http/http.dart' as http;
Run Code Online (Sandbox Code Playgroud)

然后使用以下方法解析 Uri 的链接:

var url = Uri.parse('https://www.googleapis.com/books/v1/volumes?q={http}');
http.Response response = await http.get(url);
try {
  if (response.statusCode == 200) {
    String data = response.body;
    var decodedData = jsonDecode(data);
    return decodedData;
  } else {
    return 'failed';
  }
} catch (e) {
  return 'failed';
}
Run Code Online (Sandbox Code Playgroud)


小智 5

如果仍然不起作用,请尝试以下操作:

import 'package:http/http.dart';

var response = get(Uri.parse('https://www.google.com'));
Run Code Online (Sandbox Code Playgroud)


小智 0

首先,检查您的 pubspec.yaml 文件和 HTTP 版本。它应该是您可以在这里找到的实际版本: https: //pub.dev/packages/http/install 例如,它是:

http: ^0.12.2 
Run Code Online (Sandbox Code Playgroud)

眼下

这是我的代码并且运行良好:

主程序.dart

import 'package:flutter/material.dart';
import 'package:stackowerflow/my_app.dart';

void main() {
  runApp(MyApp());
}
Run Code Online (Sandbox Code Playgroud)

my_app.dart

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

class MyApp extends StatelessWidget {
  Future<void> stackHelp() async {
    var url = 'https://www.googleapis.com/books/v1/volumes?q={http}';

    // Await the http get response, then decode the json-formatted response.
    var response = await http.get(url);
    if (response.statusCode == 200) {
      var jsonResponse = convert.jsonDecode(response.body);
      var itemCount = jsonResponse['totalItems'];
      print('Number of books about http: $itemCount.');
    } else {
      print('Request failed with status: ${response.statusCode}.');
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Flutter WebView '),
        ),
        body: Container(
          child: TextButton(onPressed: stackHelp, child: Text('Push me')),
        ),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

结果

flutter: Number of books about http: 485.

Run Code Online (Sandbox Code Playgroud)