I am saving toggle button data and adding the toggle button
data list through provider in the list of Flatdetail1. the code
is
class FormDetail1 extends StatefulWidget {
const FormDetail1({Key? key, required this.onNext}) : super(key: key);
final VoidCallback onNext;
@override
State<FormDetail1> createState() => _FormDetail1State();
}
class _FormDetail1State extends State<FormDetail1> {
List<bool> isSelected = [false, false, false];
List<bool> isSelected1 = [false, false, false, false];
List<bool> isSelected2 = [false, false, false];
String city = '';
String city1 = '';
String city2 = '';
var saveFlatDetail = FlatDetail1(
id1: '',
id2: '',
id3: '',
);
void saveData() {
Provider.of<BasicInfo>(context, listen: false).addDetails1(saveFlatDetail);
print(saveFlatDetail.id1);
print(saveFlatDetail.id2);
print(saveFlatDetail.id3);
}
@override
Widget build(BuildContext context) {
//final flatDetail = Provider.of<FlatDetail1>(context);
print('hello sir ');
return Scaffold(
appBar: AppBar(
title: Text('Flutter App'),
),
body: Column(
children: [
Center(
child: Padding(
padding: const EdgeInsets.all(50.0),
child: Column(
children: [
ToggleButtons(
isSelected: isSelected,
children: [
Text('India'),
Text('Nepal'),
Text('China'),
],
onPressed: (int newIndex) {
setState(() {
for (int i = 0; i < isSelected.length; i++) {
isSelected[i] = i == newIndex;
}
if (newIndex == 0) {
city = 'India';
} else if (newIndex == 1) {
city = 'Nepal';
} else if (newIndex == 2) {
city = 'china';
}
saveFlatDetail = FlatDetail1(
id1: city,
id2: saveFlatDetail.id2,
id3: saveFlatDetail.id3,
);
// flatDetail.updateValue(city);
// print(city);
});
},
),
ToggleButtons(
isSelected: isSelected1,
children: [
Text('UP'),
Text('MP'),
Text('Bihar'),
Text('Odisha'),
],
onPressed: (int newIndex1) {
setState(() {
for (int i = 0; i < isSelected1.length; i++) {
isSelected1[i] = i == newIndex1;
}
if (newIndex1 == 0) {
city1 = 'UP';
} else if (newIndex1 == 1) {
city1 = 'MP';
} else if (newIndex1 == 2) {
city1 = 'Bihar';
} else if (newIndex1 == 3) {
city1 = 'Odisha';
}
saveFlatDetail = FlatDetail1(
id1: saveFlatDetail.id1,
id2: city1,
id3: saveFlatDetail.id3,
);
// flatDetail.updateValue(city1);
// print(city1);
});
},
),
Container(
color: Colors.red[50],
padding: EdgeInsets.all(8),
child: ToggleButtons(
isSelected: isSelected2,
children: [
Text('Patna'),
Text('Delhi'),
Text('Lucknow'),
],
onPressed: (int newIndex2) {
setState(() {
for (int i = 0; i < isSelected2.length; i++) {
isSelected2[i] = i == newIndex2;
}
if (newIndex2 == 0) {
city2 = 'Patna';
} else if (newIndex2 == 1) {
city2 = 'Delhi';
} else if (newIndex2 == 2) {
city2 = 'Lucknow';
}
saveFlatDetail = FlatDetail1(
id1: saveFlatDetail.id1,
id2: saveFlatDetail.id2,
id3: city2,
);
});
},
),
),
SizedBox(height: 100),
GestureDetector(
onTap: () {
saveData();
if (city == '' || city1 == '' || city2 == '') {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
'Please select all',
),
duration: Duration(milliseconds: 400),
),
);
} else {
widget.onNext();
}
},
child: Container(
height: 40,
width: 125,
color: Colors.blue,
child: Text('Widget Playground!'),
),
),
SizedBox(
height: 40,
),
],
),
),
),
],
),
);
}
}
Now i want to access id1,id2 and id3 in a text field but not able to access it, here is the code below
class BasicInfo extends ChangeNotifier {
List<FlatDetail1> _details1 = [
FlatDetail1(
id1: '',
id2: '',
id3: '',
),
];
List<FlatDetail1> get details1 {
return [..._details1];
}
void addDetails1(FlatDetail1 flatDetail) {
final newFlatDetail = FlatDetail1(
id1: flatDetail.id1,
id2: flatDetail.id2,
id3: flatDetail.id3,
);
_details1.add(newFlatDetail);
notifyListeners();
}
}
When i use Text(name.details1['id1']), it shows error i also used name.details[index].id1 but still no use.Please tell me how to access the id1,id2,id3.
class FormDetail2 extends StatefulWidget {
const FormDetail2({Key? key, required this.onNext}) : super(key: key);
final VoidCallback onNext;
@override
State<FormDetail2> createState() => _FormDetail2State();
}
class _FormDetail2State extends State<FormDetail2> {
@override
Widget build(BuildContext context) {
final name = Provider.of<BasicInfo>(context);
//print(name.id1);
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(name.details1[].id1),
Text(''),
Text(''),
],
),
),
);
}
}
this is the code and i wwant to access id1,id2,id3 in FormDetail2 page