我正在使用 flutter stepper 为我的应用实现注册表单。我想在警报对话框中显示每个步骤的表单字段的值。表单字段的值显示在对话框中。但是,在后台显示以下错误。
flutter:构建 Builder 时抛出以下断言(脏,依赖项:
flutter :[_LocalizationsScope-[GlobalKey#27fdc], _InheritedTheme]:
flutter: 'package:flutter/src/material/stepper.dart': 断言失败:第 148 行 pos 15: '0 <= currentStep && flutter: currentStep < steps.length': 不正确。
flutter:断言表明框架本身存在错误,或者我们应该在此错误消息中提供更多信息,以帮助您确定和修复根本原因。
flutter:无论哪种情况,请通过在 GitHub 上提交错误来报告此断言: flutter :https://github.com/flutter/flutter/issues/new?template=BUG.md
这是我尝试在步进器中实现表单的代码:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class MyData {
String name = '';
String phone = '';
String email = '';
String age = '';
String address='';
}
MyData data = new MyData();
class _MyAppState extends State<MyApp> {
int _currentStep = 0;
List<GlobalKey<FormState>> _formKeys = [GlobalKey<FormState>(), GlobalKey<FormState>(),GlobalKey<FormState>()];
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'App',
home: Scaffold(
appBar: AppBar(title: Text('App')),
body: Builder(builder:(context)=>
Stepper(
type: StepperType.horizontal,
currentStep: _currentStep,
onStepTapped: (int step) => setState(() => _currentStep = step),
onStepContinue:(){
if (_formKeys[_currentStep].currentState.validate())
{
if (_currentStep == 0) {
_formKeys[0].currentState.save();
setState(() => _currentStep ++);
}
else if(_currentStep == 1){
_formKeys[1].currentState.save();
setState(() => _currentStep ++);
}
else if (_currentStep == 2){
_formKeys[2].currentState.save();
setState(() => _currentStep ++);
showDialog(
context: context,
builder: (BuildContext context) => AlertDialog(
title: Text("Details"),
//content: Text("Hello World"),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text("Name : " + data.name),
Text("Phone : " + data.phone),
Text("Email : " + data.email),
Text("Age : " + data.age),
Text("Email : " + data.address),
],
),
),
actions: <Widget>[
FlatButton(
child: Text('OK'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
),
);
}
}
},
onStepCancel: _currentStep > 0 ? () => setState(() => _currentStep -= 1) : null,
steps: <Step>[
Step(
title: Text('start'),
isActive: _currentStep >= 0,
state: _currentStep >= 0 ? StepState.complete : StepState.disabled,
content: Form(
key: _formKeys[0],
child: TextFormField(
keyboardType: TextInputType.text,
// autocorrect: false,
onSaved: (String value) {
data.name = value;
},
validator: (String value) {
if (value.isEmpty || value.length < 1) {
return 'Please enter name';
}
else{
return null;
}
},
decoration: new InputDecoration(
labelText: 'Enter your name',
hintText: 'Enter a name',
//filled: true,
labelStyle:
new TextStyle(decorationStyle: TextDecorationStyle.solid)),
),
),
),
Step(
title:Text('middle'),
isActive: _currentStep >= 0,
state: _currentStep >= 1 ? StepState.complete : StepState.disabled,
content: Form(
key: _formKeys[1],
child:Column(children: <Widget>[
TextFormField(
keyboardType: TextInputType.text,
autocorrect: false,
onSaved: (String value) {
data.phone= value;
},
validator: (String value) {
if (value.isEmpty || value.length < 1) {
return 'Please enter Phone number';
}
else{
return null;
}
},
decoration: new InputDecoration(
labelText: 'Enter your number',
hintText: 'Enter a number',
//filled: true,
labelStyle:
new TextStyle(decorationStyle: TextDecorationStyle.solid)),
),
TextFormField(
keyboardType: TextInputType.text,
autocorrect: false,
onSaved: (String value) {
data.email = value;
},
validator: (String value) {
if (value.isEmpty || value.length < 1) {
return 'Please enter email';
}
else{
return null;
}
},
decoration: new InputDecoration(
labelText: 'Enter your email',
hintText: 'Enter a email',
//filled: true,
labelStyle:
new TextStyle(decorationStyle: TextDecorationStyle.solid)),
),
],
)
),
),
Step(
title:Text('end'),
isActive: _currentStep >= 0,
state: _currentStep >= 2 ? StepState.complete : StepState.disabled,
content: Form(
key:_formKeys[2],
child: Column(children: <Widget>[
TextFormField(
keyboardType: TextInputType.text,
autocorrect: false,
onSaved: (String value) {
data.age= value;
},
validator: (String value) {
if (value.isEmpty || value.length < 1) {
return 'Please enter age';
}
else{
return null;
}
},
decoration: new InputDecoration(
labelText: 'Enter your age',
hintText: 'Enter a age',
//filled: true,
labelStyle:
new TextStyle(decorationStyle: TextDecorationStyle.solid)),
),
TextFormField(
keyboardType: TextInputType.text,
autocorrect: false,
onSaved: (String value) {
data.address = value;
},
validator: (String value) {
if (value.isEmpty || value.length < 1) {
return 'Please enter address';
}
else{
return null;
}
},
decoration: new InputDecoration(
labelText: 'Enter your addres',
hintText: 'Enter a address',
//filled: true,
labelStyle:
new TextStyle(decorationStyle: TextDecorationStyle.solid)),
),
],
),
),
),
],
),
),
),);
}}
最佳答案
发生错误是因为您在 Stepper 上设置了越界步骤。在 onStepContinue: (){ ... }
上删除 _currentStep++
,如错误中所指出的:
'0 <= currentStep && flutter: currentStep < steps.length': is not true.
后面发生的事情是 Stepper 试图显示一个不存在的 Step。
if (_formKeys[_currentStep].currentState!.validate()) {
if (_currentStep == 0) {
_formKeys[0].currentState!.save();
setState(() => _currentStep++);
} else if (_currentStep == 1) {
_formKeys[1].currentState!.save();
setState(() => _currentStep++);
} else if (_currentStep == 2) {
_formKeys[2].currentState!.save();
// setState(() => _currentStep++); // line causing the issue
...
}
}
关于flutter - 在 flutter 中使用步进器实现注册表单时如何修复 "[_LocalizationsScope-[GlobalKey#27fdc], _InheritedTheme])"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56616516/