提问者:小点点

我不能将URL链接分配给http. get()[重复]


我尝试使用超文本传输协议包,但我无法将URL链接分配给http. get(),它表明我无法将字符串分配给参数Uri。

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

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

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

class _MyHomePageState extends State<MyHomePage> {
  Future<List<User>> _getUsers(){
    http.get("http://www.json-generator.com/api/json/get/crbzxbbLIi?indent=2");
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: new Text(widget.title),
      ),
      body: Container(

      ),
    );
  }
}

共2个答案

匿名用户

要将URL字符串转换为Uri,只需使用Uri. parse(url)

代码示例

class _MyHomePageState extends State<MyHomePage> {
  Future<List<User>> _getUsers() {
    http.get(Uri.parse("http://www.json-generator.com/api/json/get/crbzxbbLIi?indent=2"));
  }

 // ...
}

匿名用户

您必须使用Uri:

https://flutter.dev/docs/cookbook/networking/fetch-data

Future<http.Response> fetchAlbum() {
  return http.get(Uri.https(w.json-generator.com/api/json/get/crbzxbbLIi?indent=2"));
}