I am implementing a signup form for my app using flutter stepper. I want to show the value of each step's form field in a alertdialogbox. The values of the form field are showing in the dialogbox. But, in the background it shows the following error.
flutter: The following assertion was thrown building Builder(dirty, dependencies:
flutter: [_LocalizationsScope-[GlobalKey#27fdc], _InheritedTheme]):
flutter: 'package:flutter/src/material/stepper.dart': Failed assertion: line 148 pos 15: '0 <= currentStep && flutter: currentStep < steps.length': is not true.
flutter: Either the assertion indicates an error in the framework itself, or we should provide substantially more information in this error message to help you determine and fix the underlying cause.
flutter: In either case, please report this assertion by filing a bug on GitHub: flutter: https://github.com/flutter/flutter/issues/new?template=BUG.md
Here is the code I tried for implementing form in stepper:
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)),
),
],
),
),
),
],
),
),
),);
}}