不能将参数类型“String”分配给参数类型“Uri”。我是新来的

时间:2021-05-06 20:42:42

标签: flutter dart

import 'package:flutter/material.dart';

var this_year = DateTime.now().year.toString();

class AppConfig {
  static String copyright_text = "@ SaavaTech" + this_year; //this shows in the splash 
  screen
  static String app_name = "SaavaTech"; //this shows in the splash screen

  //configure this
  static const bool HTTPS = false;

  //configure this
  // static const DOMAIN_PATH = "192.168.0.106/ecommerce_demo";
  static const DOMAIN_PATH = "shop.muwongehassan.com";
  //static const DOMAIN_PATH = "adbuild.ae";

  //do not configure these below
  static const String API_ENDPATH = "api/v2";
  static const String PUBLIC_FOLDER = "public";
  static const String PROTOCOL = HTTPS ? "https://" : "http://";
  static const String RAW_BASE_URL = "${PROTOCOL}${DOMAIN_PATH}";
  static const String BASE_URL = "${RAW_BASE_URL}/${API_ENDPATH}";

  //configure this if you are using amazon s3 like services
  //give direct link to file like https://[[bucketname]].s3.ap-southeast-1.amazonaws.com/
  //otherwise do not change anythink
  static const String BASE_PATH = "${RAW_BASE_URL}/${PUBLIC_FOLDER}/";
}

我正在尝试使用 flutter 插件 HTTP 发出 HTTP POST 请求,但出现标题错误。有没有人知道这是什么原因,因为在我的其他应用程序中这工作得很好?

final response = await http.post("${AppConfig.BASE_URL}/auth/signup",
headers: {"Content-Type": "application/json"}, body: post_body);

    

我正在尝试从 app_config.dart 文件访问 url 链接

2 个答案:

答案 0 :(得分:0)

使用:

final response = await http.post(Uri.parse("${AppConfig.BASE_URL}/auth/signup"), headers: {"Content-Type": "application/json"}, body: post_body);

答案 1 :(得分:0)

我假设您使用的是 http package。从 0.13 版开始,就有了 a breaking change。 API 已更改,现在为 url 参数接受 Uri 对象而不是 String。 仍有许多过时的教程和帖子使用 String 而不是 Uri

您可以按如下方式创建 Uri

final url = Uri.parse("${AppConfig.BASE_URL}/auth/signup");

之后,在 post 方法中使用它:

final response = await http.post(
  url,
  headers: {"Content-Type": "application/json"},
  body: post_body,
);