我正在使用flutter,nodejs和mongo来构建一个注册/登录应用程序。我对这些还很陌生,所以这个问题可能看起来有点愚蠢。我不知道如何将flutter前端连接到后端。我已经尝试过了,我得到了错误。在我的flutter注册和登录页面中,我试图连接我的服务器,我得到了这个错误:[![在这里输入图像描述][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.vpn_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),
),
),
],
))
],
),
),
)
],
));
}
}
如何摆脱此错误?
最新版本的超文本传输协议需要URI
而不是String
。所以你需要解析它。
final response = await http.get(Uri.parse(url));