【问题标题】:The argument type 'String' can't be assigned to the parameter type 'Uri' in Flutter [duplicate]Flutter中的参数类型“String”无法分配给参数类型“Uri”[重复]
【发布时间】:2021-06-10 23:11:37
【问题描述】:

我正在使用 Flutter,我正在尝试将我的应用程序连接到数据库,但是我可以连接,因为在这句话中(在 url 中)总是出现该错误:

var response = await http.post(url, body: json.encode(data));

,有谁知道问题出在哪里?代码如下:

class LoginUserState extends State {

  // For CircularProgressIndicator.
  bool visible = false ;

  // Getting value from TextField widget.
  final emailController = TextEditingController();
  final passwordController = TextEditingController();

Future userLogin() async{

  // Showing CircularProgressIndicator.
  setState(() {
  visible = true ; 
  });

  // Getting value from Controller
  String email = emailController.text;
  String password = passwordController.text;

  // SERVER LOGIN API URL
  var url = 'https://fluer-examples.com/login_user.php';

  // Store all data with Param Name.
  var data = {'email': email, 'password' : password};

  // Starting Web API Call.
  var response = await http.post(url, body: json.encode(data));

  // Getting Server response into variable.
  var message = jsonDecode(response.body);

  // If the Response Message is Matched.
  if(message == 'Login Matched')
  {

    // Hiding the CircularProgressIndicator.
      setState(() {
      visible = false; 
      });

    // Navigate to Profile Screen & Sending Email to Next Screen.
      Navigator.push(
        context,
        MaterialPageRoute(builder: (context) => ProfileScreen(email : emailController.text))
      );
  }else{

    // If Email or Password did not Matched.
    // Hiding the CircularProgressIndicator.
    setState(() {
      visible = false; 
      });

    // Showing Alert Dialog with Response JSON Message.
    showDialog(
    context: context,
    builder: (BuildContext context) {
      return AlertDialog(
        title: new Text(message),
        actions: <Widget>[
          FlatButton(
            child: new Text("OK"),
            onPressed: () {
              Navigator.of(context).pop();
            },
          ),
        ],
      );
    },
    );}

}

【问题讨论】:

  • 那是什么错误?那也很有帮助!

标签: php database flutter dart


【解决方案1】:

当您遇到此类问题时,阅读documentation 总是很好。在http post方法签名中:

Future<Response> post (
Uri url,
{Map<String, String>? headers,
Object? body,
Encoding? encoding}
)

第一个参数是 Uri 类型,而不是 String。这是关于Uri的文档

您将需要使用 Url.http 方法来创建 URI 并传入您的 url:

Uri.http(
String authority,
String unencodedPath,
[Map<String, dynamic>? queryParameters]
)

示例 - Uri.http("https://fluer-examples.com/login_user.php", "");

【讨论】:

  • 行得通!谢谢
  • 这不是Uri.http 的用途。如果它碰巧有效,我不会依赖它继续工作。真的,您应该改用Uri.parse
  • 请参阅stackoverflow.com/q/66619895,了解使用Uri.http/Uri.https 是错误的示例。
猜你喜欢
  • 2021-11-23
  • 2021-06-27
  • 2021-10-17
  • 2021-07-06
  • 2020-10-22
  • 2021-06-19
  • 1970-01-01
  • 2021-05-13
相关资源
最近更新 更多