【问题标题】:Flutter throwing error when used the get method in the http package使用http包中的get方法时Flutter抛出错误
【发布时间】:2021-04-25 10:14:28
【问题描述】:

当我尝试通过 Flutter http 包提供的 get 方法从 Internet 获取数据以在我的应用程序中使用它时,它会引发此错误 - 参数类型“字符串”无法分配给参数类型“Uri”。这是我的代码

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



class Loading extends StatefulWidget {
  @override
  _LoadingState createState() => _LoadingState();
}

class _LoadingState extends State<Loading> {
  @override
  void getData() async {
   http.get("https://jsonplaceholder.typicode.com/todos/1")
  }

  void initState() {
    super.initState();
    getData();
  }

  Widget build(BuildContext context) {
    return Scaffold(
      body: Text("some text"),
    );
  }
}

【问题讨论】:

标签: flutter dart flutter-dependencies


【解决方案1】:

http包请求方法的第一个参数是Uri类型,所以你得把你的代码改成这样:

  void getData() async {
    final requestUrl = Uri.parse("https://jsonplaceholder.typicode.com/todos/1");
    http.get(requestUrl)
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-18
    • 2016-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-25
    • 1970-01-01
    • 2020-01-31
    相关资源
    最近更新 更多