I have passed the data in a variable of the previous screen to the next using the guidance of the chosen answer in this post: Passing data between screens in Flutter
That variable should be encoded into JSON along with the data gathered from the form in this screen, but it is not identified there. What is going on here? I thought a variable declared inside the global class was identified within it. Thank you for any guidance you can give me. The code is as follows:
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
class CustomerForm extends StatefulWidget {
final String askLoan;
const CustomerForm({Key key, @required this.askLoan}) : super(key: key);
@override
CustomerFormState createState() {
return CustomerFormState();
}
}
final branchList = ['අගලවත්ත', 'ආගරපතන', 'යක්කලමුල්ල', 'යටියන්තොට'];
class CustomerFormState extends State<CustomerForm> {
final formKey = GlobalKey<FormState>();
String _name = '';
String _age = '';
String _nic = '';
String _sex = '';
String _telephone = '';
String _address = '';
String _email = '';
String _inquiry = '';
String _branch = '';
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: false,
appBar: AppBar(
title: Text(
'ඔබේ තොරතුරු පහත පුරවන්න',
style: TextStyle(
fontSize: (20.0),
),
),
centerTitle: true,
backgroundColor: Colors.cyan,
shadowColor: Colors.tealAccent[50]),
body: SingleChildScrollView(
child: Form(
key: formKey,
child: Column(children: [
.............
ElevatedButton(
onPressed: () {
if (_sex.isNotEmpty &&
_branch.isNotEmpty &&
formKey.currentState.validate()) {
formKey.currentState.save();
final message = '$_name, විමසීම සාර්ථකයි.';
final snackBar = SnackBar(
content: Text(message),
backgroundColor: Colors.blue,
duration: Duration(milliseconds: 3000),
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
return http.post(
Uri.parse('http://10.0.2.2:5000/api/userdata'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, String>{
'name': _name,
'age': _age,
'NIC': _nic,
'sex': _sex,
'tel': _telephone,
'addr': _address,
'email': _email,
'inquiry': _inquiry,
'branch': _branch,
'askloan': askLoan,
}));
} else {
final message =
'කරුණාකර සියළු තොරතුරු සපයා ඇතිදැයි යළි පරීක්ෂා කරන්න.';
final snackBar = SnackBar(
content: Text(message),
backgroundColor: Colors.deepOrange,
duration: Duration(milliseconds: 4000),
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
},
child: Text('ඔබේ විමසීම මෙතැනින් අප වෙත යොමුකරන්න.'),
),
Padding(
padding: const EdgeInsets.fromLTRB(0, 0, 0, 25),
),
]),
),
));
}
}