我有一个具有不同功能的应用程序,现在我想将它们全部加在一起,并且正在使用scoped-model。
我有一个名为user的类,并且我有一些变量和一个函数来发出http发布请求(使用用户名+密码)。
每当我使用以下登录功能时,我都会出错。 > ##[ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: NoSuchMethodError: The getter 'currentState' was called on null.##
我尝试了不同的方法,但是我的模型中没有吸气剂,我认为情况并非如此,我只看到带有私有变量的吸气剂。如果我将变量和函数也放在main.dart文件中,则该函数正常工作。用户类别
User.Dart
class User extends Model {
String url = "url"
Future<String> submit() async {
{ var response =
await http.post(Uri.encodeFull(url), body: json.encode({}), headers: {
"content-type": "application/json",
"Accept": "application/json",
"Authorization": **encrypted username+password**
});
if (responsemessage == "OK") {
print('token is goed $token');
} else {
print('token is fout');
}
notifyListeners();
}
这是main.dart文件中的代码
Main.dart
import 'package:flutter/material.dart';
import 'package:scoped_model/scoped_model.dart';
import 'package:test_models/model/user.dart';
void main() => runApp(MyApp(user: User()));
class MyApp extends StatelessWidget {
final User user;
const MyApp({Key key, this.user}) : super(key: key);
@override
Widget build(BuildContext context) {
return ScopedModel<User>(
model: user,
child: MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
children: <Widget>[
ScopedModelDescendant<User>(builder: (context, child, model) {
return Column(
children: <Widget>[
Card(
child: Padding(
padding: EdgeInsets.all(8.0),
child: Form(
key: formKey,
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
TextFormField(
decoration: InputDecoration(labelText: 'Email:'),
onSaved: (input) => model.onsavedemail = input,
),
TextFormField(
decoration: InputDecoration(labelText: 'Password:'),
onSaved: (input) => model.onsavedpassword = input,
obscureText: true,
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: RaisedButton(
onPressed: model.submit,
// {Navigator.of(context).pushNamed("/Secondpage")}
child: Text('Sign in'),
),
)
],
),
],
),
),
)
)
],
);
}),
]
),
),
);
}
}
希望有人可以告诉我我做错了什么,并向我提供解释,以便我在要实现到我的应用程序中的下一个功能中使用它。