我试图创建一个认证的安多德应用程序使用颤振黑暗。我为登录注册路由创建了一个带有端点API的节点js服务器,它直接与阿特拉斯MongoDB数据库通信。我可以在邮递员中成功地发送POST请求,但是当我试图从颤振应用程序中调用它时,我会得到一个错误,如下所示
在3,634 in中重新启动应用程序。I/颤振(10513):TextFormField I/颤振(10513):TextFormField E/颤振(10513):错误:flutter/lib/ui/ui_dart_state.cc(186个)未处理的异常:类型'TextFormField‘不是'String’的子类型[38;5;248 39/颤振(10513):#0登录[39;49m包:cop_app/登录_e.dart][38;5;248 39/颤音(10513)]:#1 LoginSection.build。[39;49m包:cop_app_page.dart[38;5;5;248 38/颤振(10513):#2 LoginSection.build。[39;49m包:cop_app/登录_page.dart][38;5;244 38/颤振(10513):#3 ]
_InkResponseState._handleTap[39;49m包:颤振/…][38;5;244 38/颤振(10513):#4 GestureRecognizer.invokeCallback[39;49m包:颤振/…]/手势/识别器[38;5;244 38/颤振(10513):#5 TapGestureRecognizer.handleTapUp[39;49m包:颤振/…]/手势/踢踏舞[38;5;244 38/颤振(10513):#6
BaseTapGestureRecognizer._checkUp[39;49m包:颤振/…]/手势/飞镖[38;5;244 38/颤振(10513):#7
BaseTapGestureRecognizer.acceptGesture[39;49m包:颤振/…]/手势/踢踏舞[38;5;244 38/颤振(10513):#8
GestureArenaManager.sweep[39;49m包:颤振/…]/手势/arena.dart[38;5;244 38/颤振(10513):#9 GestureBinding.handleEvent[39;49m包:颤振/…]/手势/绑定.飞镖[38;5;244 38/颤振(10513):#10 GestureBinding.dispatchEvent[39;49m包:颤振/…]/手势/绑定.飞镖[38;5;244 38/颤振(10513):#11 RendererBinding.dispatchEvent[39;49m包:颤振/…]/呈现/绑定.10513[38;5;244…/颤振(10513):#12 GestureBinding._handlePointerEventImmediately[39;49m包:flutter/…/手势/绑定.飞镖[38;5;244 38/颤振(10513):#13 GestureBinding.handlePointerEvent[39;49m包:颤振/…]/手势/绑定.飞镖[38;5;244 38/颤振(10513):#14 GestureBinding._flushPointerEventQueue[39;49m包:颤振/…]/手势/绑定.飞镖[38;5;244 38/颤振(10513):#15 GestureBinding._handlePointerDataPacket[39;49m包:颤振/…/手势/绑定.飞镖[38;5;244 16/颤振(10513):#16 _rootRunUnary (飞镖:异步/zone.dart:1370:13)[39;49m[38;5;54 16/颤振(10513)]:#17 _CustomZone.runUnary (飞镖:异步/飞镖:1265:19)[39;49m;5;244 16/颤振(10513):#18 _CustomZone.runUnaryGuarded (飞镖:异步/zone.dart:1170:7)][39;49;49;5;5;244 20/颤振(10513):#19 _invoke1 (飞镖:ui/钩子:180:10)[39;49m][38;5;244 20/颤振(10513):#20
(dart:ui/platform_dispatcher.dart:276:7)[39;49m [38;5;244 39/颤振(10513):#21 _dispatchPointerDataPacket (飞镖:ui/钩子:96:31)[39;49m E/颤振(10513):
这是我的密码
import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';
import 'profile.dart';
class LoginSection extends StatelessWidget {
static const String id = "LoginSection";
var uid;
var password;
@override
Widget build(BuildContext context) {
final logo = Hero(
tag: 'hero',
child: CircleAvatar(
backgroundColor: Colors.transparent,
radius: 48.0,
child: Image.asset('assets/profile.png'),
),
);
final uid = TextFormField(
textAlign: TextAlign.center,
keyboardType: TextInputType.text,
autofocus: false,
initialValue: 'abc123',
decoration: InputDecoration(
hintText: 'Unique ID',
contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
border: OutlineInputBorder(borderRadius: BorderRadius.circular(32.0)),
),
);
final password = TextFormField(
textAlign: TextAlign.center,
autofocus: false,
initialValue: '123abc',
obscureText: true,
decoration: InputDecoration(
hintText: 'Password',
contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
border: OutlineInputBorder(borderRadius: BorderRadius.circular(32.0)),
),
);
final loginButton = Padding(
padding: EdgeInsets.symmetric(vertical: 16.0),
child: RaisedButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(24),
),
onPressed: () async {
await login(uid, password);
SharedPreferences prefs = await SharedPreferences.getInstance();
String token = prefs.getString("token");
print(token);
if (token != null) {
Navigator.popAndPushNamed(context, LandingScreen.id);
}
},
padding: EdgeInsets.all(12),
color: Colors.lightBlueAccent,
child: Text('Log In', style: TextStyle(color: Colors.white)),
),
);
final forgotLabel = FlatButton(
child: Text(
'Forgot password?',
style: TextStyle(color: Colors.black54),
),
onPressed: () {},
);
return Scaffold(
backgroundColor: Colors.white,
body: Center(
child: ListView(
shrinkWrap: true,
padding: EdgeInsets.only(left: 24.0, right: 24.0),
children: <Widget>[
logo,
SizedBox(height: 48.0),
uid,
SizedBox(height: 8.0),
password,
SizedBox(height: 24.0),
loginButton,
forgotLabel
],
),
),
);
}
}
login(uid, password) async {
print(uid);
print(password);
var url = "http://192.147.111.104:5000/login"; // iOS
final http.Response response = await http.post(
url,
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, String>{
'uid': uid,
'password': password,
}),
);
print(response.body);
SharedPreferences prefs = await SharedPreferences.getInstance();
var parse = jsonDecode(response.body);
await prefs.setString('token', parse["token"]);
}我无法理解这里的问题,我引用了这个源代码作为发送post请求的代码片段,我搜索了类似的问题,但是找不到任何与我的问题相关的解决方案,有人能帮我解决这个问题吗?
发布于 2021-02-08 15:30:43
TextFormField是表单中用来收集用户输入的小部件。
https://api.flutter.dev/flutter/material/TextFormField-class.html
作为参数传递给小部件的TextEditingController是如何访问用户输入的。
https://api.flutter.dev/flutter/widgets/TextEditingController-class.html
在您执行post请求的情况下,您将希望访问controller.text,而不是传递整个小部件。
class VeryBasicForm extends StatefulWidget {
@override
_VeryBasicFormState createState() => _VeryBasicFormState();
}
class _VeryBasicFormState extends State<VeryBasicForm> {
final uidController = TextEditingController();
final passwordController = TextEditingController();
@override
void initState(){
// set initial value here
uidController.text = 'abc123';
passwordController.text = 'abc123';
super.initState();
}
@override
void dispose() {
uidController.dispose();
passwordController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Column(
children: [
TextFormField(
textAlign: TextAlign.center,
controller: uidController,
keyboardType: TextInputType.text,
autofocus: false,
decoration: InputDecoration(
hintText: 'Unique ID',
contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
border: OutlineInputBorder(borderRadius: BorderRadius.circular(32.0)),
),
),
TextFormField(
textAlign: TextAlign.center,
controller: passwordController,
autofocus: false,
obscureText: true,
decoration: InputDecoration(
hintText: 'Password',
contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
border: OutlineInputBorder(borderRadius: BorderRadius.circular(32.0)),
),
),
Padding(
padding: EdgeInsets.symmetric(vertical: 16.0),
child: RaisedButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(24),
),
onPressed: () async {
// pass in the String values from inputs using the controllers
await login(uidController.text, passwordController.text);
SharedPreferences prefs = await SharedPreferences.getInstance();
String token = prefs.getString("token");
print(token);
if (token != null) {
Navigator.popAndPushNamed(context, LandingScreen.id);
}
},
padding: EdgeInsets.all(12),
color: Colors.lightBlueAccent,
child: Text('Log In', style: TextStyle(color: Colors.white)),
),
);
],
);
}
}发布于 2021-02-08 15:10:34
您正在将一个TextFormField小部件传递给我认为您想成为文本的东西。先定义一个控制器,
final myPasswordController = TextEditingController();
final myUIDController = TextEditingController();然后在您的函数中使用它,如
final password = TextFormField(
textAlign: TextAlign.center,
controller: myPassWordController,
autofocus: false,
obscureText: true,
decoration: InputDecoration(
hintText: 'Password',
contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
border: OutlineInputBorder(borderRadius: BorderRadius.circular(32.0)),
),
);
final uid = TextFormField(
textAlign: TextAlign.center,
controller: myUIDController,
keyboardType: TextInputType.text,
autofocus: false,
decoration: InputDecoration(
hintText: 'Unique ID',
contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
border: OutlineInputBorder(borderRadius: BorderRadius.circular(32.0)),
),
);然后在你被压抑;
await login(myPasswordController.text, myPasswordController.text);https://stackoverflow.com/questions/66103977
复制相似问题