Flutter 中无法将参数类型 'String' 分配给参数类型 'Uri'

时间:2021-03-12 22:55:51

标签: php database flutter dart

我正在使用 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();
            },
          ),
        ],
      );
    },
    );}

}

1 个答案:

答案 0 :(得分:1)

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

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

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

的文档

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

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

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