错误:无法将参数类型“字符串”分配给参数类型“Uri”。使用 JsonPlaceHolder

时间:2021-04-19 16:19:40

标签: flutter flutter-dependencies

error image
我正在尝试运行此代码但出现错误,有人可以帮忙吗?我尝试制作字符串,但仍然出现相同的错误。

错误:无法将参数类型“String”分配给参数类型“Uri”。 (argument_type_not_assignable at [world_time] lib\pages\loading.dart:17)enter image description here

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

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

class _LoadingState extends State<Loading> {


  void getData() async {
    Response response = await get(
        'https://jsonplaceholder.typicode.com/todos/1');
    print(response.body);
  }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Text('loading screen'),
    );
  }?
}

3 个答案:

答案 0 :(得分:1)

get(Uri.parse('https://jsonplaceholder.typicode.com/todos/1'));

get(Uri.https('jsonplaceholder.typicode.com', '/todos/1'));

答案 1 :(得分:0)

您可以使用以下方法解决问题:

Uri.parse('https://jsonplaceholder.typicode.com/todos/1')

答案 2 :(得分:0)

我正在为我的应用程序制作一个 http get,但出现错误“无法将参数类型‘字符串’分配给参数类型‘Uri’”。 这是我的代码:

class MapsUtil {
  static const BASE_URL = 'https://maps.googleapis.com/maps/api/directions/json?';

  static MapsUtil _instance = new MapsUtil.internal();

  MapsUtil.internal();

  factory MapsUtil() => _instance;
  final JsonDecoder _decoder = new JsonDecoder();

  Future<dynamic> get(String url) {
    return http.get(BASE_URL + url).then((http.Response response) {
      String res = response.body;
      int statusCode = response.statusCode;
//      print("API Response: " + res);
      if (statusCode < 200 || statusCode > 400 || json == null) {
        res = "{\"status\":" + statusCode.toString() + ",\"message\":\"error\",\"response\":" + res + "}";
        throw new Exception(res);
      }

      List<LatLng> steps;
      try {
        steps = parseSteps(_decoder.convert(res)["routes"][0]["legs"][0]["steps"]);
      } catch (e) {
        print(CustomTrace(StackTrace.current, message: e));
        // throw new Exception(e);
      }

      return steps;
    });
  }

  List<LatLng> parseSteps(final responseBody) {
    List<Step> _steps = responseBody.map<Step>((json) {
      return new Step.fromJson(json);
    }).toList();
    List<LatLng> _latLang = _steps.map((Step step) => step.startLatLng).toList();
    return _latLang;
  }

  Future<String> getAddressName(LatLng location, String apiKey) async {
    try {
      var endPoint =
          'https://maps.googleapis.com/maps/api/geocode/json?latlng=${location?.latitude},${location?.longitude}&language=${setting.value.mobileLanguage.value}&key=$apiKey';
      var response = jsonDecode((await http.get(endPoint, headers: await LocationUtils.getAppHeaders())).body);

      return response['results'][0]['formatted_address'];
    } catch (e) {
      print(CustomTrace(StackTrace.current, message: e));
      return null;
    }
  }
}

我正在尝试许多可能的解决方案,但我遇到了同样的错误