Flutter 即使没有抛出异常,Snackbar也不会显示
我有下面的代码发送POST请求,并且在得到响应后必须显示一个Snackbar,但是屏幕上没有显示任何内容。这个代码有什么问题 主飞镖Flutter 即使没有抛出异常,Snackbar也不会显示,flutter,Flutter,我有下面的代码发送POST请求,并且在得到响应后必须显示一个Snackbar,但是屏幕上没有显示任何内容。这个代码有什么问题 主飞镖 import 'package:ahgora/screens/home.dart'; import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { // This widget is the r
import 'package:ahgora/screens/home.dart';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: Home(),
);
}
}
霍姆·达特
import 'package:ahgora/models/user.dart';
import 'package:flutter/material.dart';
import 'dart:convert';
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
final _user = User();
final _formKey = GlobalKey<FormState>();
final _tokenNode = FocusNode();
final _userNode = FocusNode();
final _passwordNode = FocusNode();
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: true,
body: Container(
child: Builder(
builder: (context) => Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
child: Image.asset("assets/images/logo.png",
fit: BoxFit.scaleDown,
width: 160,
alignment: Alignment.center),
margin: EdgeInsets.only(top: 100.0),
),
Expanded(
child: ListView(
children: <Widget>[
Container(
margin: EdgeInsets.all(20.0),
child: TextFormField(
textInputAction: TextInputAction.next,
focusNode: _tokenNode,
onFieldSubmitted: (term) {
_fieldFocusChange(context, _tokenNode, _userNode);
},
onSaved: (val) {
setState(() {
_user.identity = val;
});
},
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Chave',
),
validator: (value) {
return value.isEmpty
? 'Preencha com a chave.'
: null;
},
),
),
Container(
margin: EdgeInsets.all(20.0),
child: TextFormField(
textInputAction: TextInputAction.next,
focusNode: _userNode,
onFieldSubmitted: (term) {
_fieldFocusChange(
context, _userNode, _passwordNode);
},
onSaved: (val) {
setState(() {
_user.account = val;
});
},
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Usuário'),
validator: (value) {
return value.isEmpty ? 'Preencha o usuário.' : null;
},
),
),
Container(
margin: EdgeInsets.all(20.0),
child: TextFormField(
textInputAction: TextInputAction.next,
focusNode: _passwordNode,
onSaved: (val) {
setState(() {
_user.password = val;
});
},
decoration: InputDecoration(
border: OutlineInputBorder(), labelText: 'Senha'),
validator: (value) {
return value.isEmpty ? 'Preencha a senha.' : null;
},
),
),
Container(
margin: EdgeInsets.all(20.0),
child: MaterialButton(
color: Colors.redAccent,
textColor: Colors.white,
onPressed: () {
final form = _formKey.currentState;
if (form.validate()) {
form.save();
_user.register().then((response) {
if (response.statusCode == 200 && json.decode(response.body).error != true) {
final snackbar = SnackBar(content: Text('Ponto registrado'));
Scaffold.of(context).showSnackBar(snackbar);
}
else {
final snackbar = SnackBar(content: Text('Erro. Mensagem: ' + json.decode(response.body).message));
Scaffold.of(context).showSnackBar(snackbar);
}
});
}
},
child: Text('Registrar'),
),
)
],
),
),
],
),
),
),
),
);
}
_fieldFocusChange(
BuildContext context, FocusNode currentFocus, FocusNode nextFocus) {
currentFocus.unfocus();
FocusScope.of(context).requestFocus(nextFocus);
}
}
import'包:ahgora/models/user.dart';
进口“包装:颤振/材料.省道”;
导入“dart:convert”;
类Home扩展了StatefulWidget{
@凌驾
_HomeState createState()=>\u HomeState();
}
类(HomeState扩展状态){
最终用户=用户();
final _formKey=GlobalKey();
final _tokenNode=FocusNode();
final _userNode=FocusNode();
final _passwordNode=FocusNode();
@凌驾
小部件构建(构建上下文){
返回脚手架(
resizeToAvoidBottomInset:true,
主体:容器(
孩子:建筑工人(
生成器:(上下文)=>表单(
键:_formKey,
子:列(
crossAxisAlignment:crossAxisAlignment.center,
儿童:[
容器(
子项:Image.asset(“assets/images/logo.png”,
适合度:BoxFit.scaleDown,
宽度:160,
对齐:对齐。居中),
边距:仅限边缘集(顶部:100.0),
),
扩大(
子:ListView(
儿童:[
容器(
裕度:所有边缘集(20.0),
子项:TextFormField(
textInputAction:textInputAction.next,
focusNode:_令牌节点,
已提交:(任期){
_fieldFocusChange(上下文、\u令牌节点、\u用户节点);
},
保存:(val){
设置状态(){
_user.identity=val;
});
},
装饰:输入装饰(
边框:OutlineInputBorder(),
labelText:“Chave”,
),
验证器:(值){
返回值.isEmpty
?“预扎com a chave。”
:null;
},
),
),
容器(
裕度:所有边缘集(20.0),
子项:TextFormField(
textInputAction:textInputAction.next,
focusNode:_userNode,
已提交:(任期){
_现场焦点更改(
上下文、\u userNode、\u passwordNode);
},
保存:(val){
设置状态(){
_user.account=val;
});
},
装饰:输入装饰(
边框:OutlineInputBorder(),
标签文字:“Usuário”),
验证器:(值){
返回值.isEmpty?'Preencha o usuário':null;
},
),
),
容器(
裕度:所有边缘集(20.0),
子项:TextFormField(
textInputAction:textInputAction.next,
focusNode:_passwordNode,
保存:(val){
设置状态(){
_user.password=val;
});
},
装饰:输入装饰(
边框:OutlineInputBorder(),labelText:'Senha'),
验证器:(值){
返回值.isEmpty?'Preencha a senha':null;
},
),
),
容器(
裕度:所有边缘集(20.0),
子:材质按钮(
颜色:Colors.redAccent,
textColor:Colors.white,
已按下:(){
最终形式=_formKey.currentState;
if(form.validate()){
form.save();
_user.register()然后((响应){
if(response.statusCode==200&&json.decode(response.body).error!=true){
最终snackbar=snackbar(内容:文本('Ponto registrado'));
脚手架.of(上下文).showSnackBar(snackbar);
}
否则{
final snackbar=snackbar(内容:Text('Erro.mensage:'+json.decode(response.body.message));
脚手架.of(上下文).showSnackBar(snackbar);
}
});
}
},
子项:文本(“注册者”),
),
)
],
),
),
],
),
),
),
),
);
}
_现场焦点更改(
BuildContext上下文,FocusNode currentFocus,FocusNode nextFocus){
currentFocus.unfocus();
FocusScope.of(上下文).requestFocus(nextFocus);
}
}
user.dart
import 'dart:io';
import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:http/http.dart' as http;
class User {
final tokenKey = 'token_key';
final loginKey = 'login_key';
final passwordKey = 'password_key';
String identity;
String account;
String password;
read() async {
final prefs = await SharedPreferences.getInstance();
identity = prefs.getString(tokenKey);
account = prefs.getString(loginKey);
password = prefs.getString(passwordKey);
}
Future<http.Response> register() async {
final prefs = await SharedPreferences.getInstance();
prefs.setString(tokenKey, identity);
prefs.setString(loginKey, account);
prefs.setString(passwordKey, password);
final response = await http.post('https://localhost:9000/verifyIdentification',
headers: {
HttpHeaders.contentTypeHeader: 'multipart/form-data'
},
body: json.encode({
'identity': this.identity,
'account': this.account,
'password': this.password
})
);
return response;
}
}
导入'dart:io';
导入“dart:convert”;
“导入”包:共享\u首选项