错误:无法将参数类型“String”分配给参数类型“Uri”。 'Uri'来自'dart:core' [重复]

Posted

技术标签:

【中文标题】错误:无法将参数类型“String”分配给参数类型“Uri”。 \'Uri\'来自\'dart:core\' [重复]【英文标题】:Error: The argument type 'String' can't be assigned to the parameter type 'Uri'. 'Uri' is from 'dart:core' [duplicate]错误:无法将参数类型“String”分配给参数类型“Uri”。 'Uri'来自'dart:core' [重复] 【发布时间】:2021-07-06 12:47:50 【问题描述】:

我正在使用 Flutter、nodejs 和 mongo 构建一个注册/登录应用程序。我对这些还是陌生的,所以这个问题可能看起来有点愚蠢。我不知道如何准确地将颤振前端连接到后端。我已经尝试过了,但我遇到了错误。 在我尝试连接服务器的颤振注册和登录页面中,我收到此错误: [![在此处输入图片描述][1]][1]

import 'package:flutter/material.dart';
import 'package:zakatapp/signin.dart';
import 'package:zakatapp/user.dart';
import 'package:flutter_svg/svg.dart';
import 'package:http/http.dart' as http;

class Signup extends StatefulWidget 
  Signup(Key key) : super(key: key);

  @override
  _SignupState createState() => _SignupState();


class _SignupState extends State<Signup> 
  final _formKey = GlobalKey<FormState>();
  Future save() async 
    var res = await http.post("http://localhost:8080/signup",
        headers: <String, String>
          'Context-Type': 'application/json;charSet=UTF-8'
        ,
        body: <String, String>
          'email': user.email,
          'password': user.password
        );
    print(res.body);
    Navigator.push(
        context, new MaterialPageRoute(builder: (context) => Signin()));
  

  User user = User('', '');
  @override
  Widget build(BuildContext context) 
    return Scaffold(
        body: Stack(
      children: [
        Positioned(
            top: 0,
            child: SvgPicture.asset(
              'images/top.svg',
              width: 400,
              height: 150,
            )),
        Container(
          alignment: Alignment.center,
          child: Form(
            key: _formKey,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                SizedBox(
                  height: 150,
                ),
                Text(
                  "Signup",
                  style: TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: 50,
                      color: Colors.blue),
                ),
                SizedBox(
                  height: 25,
                ),
                Padding(
                  padding: const EdgeInsets.all(16.0),
                  child: TextFormField(
                    controller: TextEditingController(text: user.email),
                    onChanged: (value) 
                      user.email = value;
                    ,
                    validator: (value) 
                      if (value.isEmpty) 
                        return 'Enter something';
                       else if (RegExp(
                              r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`|~]+@[a-zA-Z0-9]+\.[a-zA-Z]+")
                          .hasMatch(value)) 
                        return null;
                       else 
                        return 'Enter valid email';
                      
                    ,
                    decoration: InputDecoration(
                        icon: Icon(
                          Icons.email,
                          color: Colors.blue,
                        ),
                        hintText: 'Enter Email',
                        enabledBorder: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(16),
                            borderSide: BorderSide(color: Colors.blue)),
                        focusedBorder: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(16),
                            borderSide: BorderSide(color: Colors.blue)),
                        errorBorder: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(16),
                            borderSide: BorderSide(color: Colors.red)),
                        focusedErrorBorder: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(16),
                            borderSide: BorderSide(color: Colors.red))),
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.all(16.0),
                  child: TextFormField(
                    controller: TextEditingController(text: user.email),
                    onChanged: (value) 
                      user.email = value;
                    ,
                    validator: (value) 
                      if (value.isEmpty) 
                        return 'Enter something';
                      
                      return null;
                    ,
                    decoration: InputDecoration(
                        icon: Icon(
                          Icons.***_key,
                          color: Colors.blue,
                        ),
                        hintText: 'Enter Password',
                        enabledBorder: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(16),
                            borderSide: BorderSide(color: Colors.blue)),
                        focusedBorder: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(16),
                            borderSide: BorderSide(color: Colors.blue)),
                        errorBorder: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(16),
                            borderSide: BorderSide(color: Colors.red)),
                        focusedErrorBorder: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(16),
                            borderSide: BorderSide(color: Colors.red))),
                  ),
                ),
                Padding(
                  padding: EdgeInsets.fromLTRB(55, 16, 16, 0),
                  child: Container(
                    height: 50,
                    width: 400,
                    child: FlatButton(
                        color: Colors.blue,
                        shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(16.0)),
                        onPressed: () 
                          if (_formKey.currentState.validate()) 
                            save();
                           else 
                            print("not ok");
                          
                        ,
                        child: Text(
                          "Signup",
                          style: TextStyle(color: Colors.white, fontSize: 20),
                        )),
                  ),
                ),
                Padding(
                    padding: const EdgeInsets.fromLTRB(95, 20, 0, 0),
                    child: Row(
                      children: [
                        Text(
                          "Already have Account ? ",
                          style: TextStyle(
                              color: Colors.black, fontWeight: FontWeight.bold),
                        ),
                        InkWell(
                          onTap: () 
                            Navigator.push(
                                context,
                                new MaterialPageRoute(
                                    builder: (context) => Signin()));
                          ,
                          child: Text(
                            "Signin",
                            style: TextStyle(
                                color: Colors.blue,
                                fontWeight: FontWeight.bold),
                          ),
                        ),
                      ],
                    ))
              ],
            ),
          ),
        )
      ],
    ));
  

如何摆脱这个错误?

【问题讨论】:

【参考方案1】:

最新版本的 http 需要 URI 而不是 String。所以你需要解析它。

final response = await http.get(Uri.parse(url));

【讨论】:

以上是关于错误:无法将参数类型“String”分配给参数类型“Uri”。 'Uri'来自'dart:core' [重复]的主要内容,如果未能解决你的问题,请参考以下文章

错误:参数类型“TextEditingController”无法分配给参数类型“String”。在 FLUTTER

Flutter SQLlite 参数类型“Future<String>”无法分配给参数类型“String”错误

Flutter中的参数类型“String”无法分配给参数类型“Uri”[重复]

错误:不能将参数类型“String”分配给参数类型“Uri”。使用JsonPlaceHolder

错误:无法将参数类型“FirebaseApp”分配给参数类型“Firebase”

错误:无法将参数类型“MaterialAccentColor”分配给参数类型“MaterialColor”