简体   繁体   English

API 和 HTTP flutter 数据

[英]API and HTTP flutter data

How to Fix this error enter image description here Error: The argument type 'String' can't be assigned to the parameter type 'Uri'.如何修复此错误enter image description here错误:无法将参数类型“String”分配给参数类型“Uri”。

  • 'Uri' is from 'dart:core'. 'Uri' 来自 'dart:core'。 await http.get('https://jsonplaceholder.typicode.com/posts/1');等待 http.get('https://jsonplaceholder.typicode.com/posts/1');

Error: Property 'title' cannot be accessed on 'Post?'错误:无法在“发布?”上访问属性“title” because it is potentially null.因为它可能是 null。

  • 'Post' is from 'package.networking/model/post.dart' ('lib/model/post.dart'). 'Post' 来自 'package.networking/model/post.dart' ('lib/model/post.dart')。 Try accessing using?.尝试访问使用?。 instead.反而。 return Text(snapshot.data.title);返回文本(snapshot.data.title); ^^^^^ lib/model/post.dart:11:17: Error: The operator '[]' isn't defined for the class 'JsonCodec'. ^^^^^ lib/model/post.dart:11:17:错误:未为 class“JsonCodec”定义运算符“[]”。
  • 'JsonCodec' is from 'dart:convert'. “JsonCodec”来自“dart:convert”。 Try correcting the operator to an existing operator, or defining a '[]' operator.尝试将运算符更正为现有运算符,或定义“[]”运算符。 id: json['id'], id: json['id'],

You need to use Uri.parse() for parse url of api like您需要使用 Uri.parse() 来解析 url 的 api,例如

http.get(
          Uri.parse('${AppConfig.apiBaseUrl}country'),
        );

http does not handle the URL strings directly starting from http: 0.13.0 . http不处理直接从http: 0.13.0开始的 URL 字符串。

You need to use either.您需要使用其中任何一个。

http.get(Uri.https(AppConfig.apiBaseUrl, 'country'));

or或者

http.get(Uri.parse('${AppConfig.apiBaseUrl}country'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM