I have the following code to enter a text into a textfield, on the onChanged()
I can see the value entered in HEX.But when I'm trying to print the value of hexString
on the onPressed()
it is empty.I need to include this value on the async to send it in a API request.Any help is appreciated.
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:flutter/material.dart';
import 'package:australremote/screens/screen4.dart';
import 'package:convert/convert.dart';
void main() {
runApp(MyHomePage());
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final _textController = TextEditingController();
final String title = '';
final String subtitle = '';
@override
Widget build(BuildContext context) {
final args = ModalRoute.of(context)?.settings?.arguments as List<String>;
String ssid = args[0];
String auth = args[1];
String hexString = '';
return Scaffold(
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("Your selected SSID is :$ssid"),
Text("Your authentication is :$auth"),
TextField(
controller: _textController,
decoration: InputDecoration(hintText: "Enter the password of your Wifi network"),
onChanged: (value) {
setState(() {
hexString = '';
for (int i = 0; i < value.length; i ) {
hexString = value[i].codeUnitAt(0).toRadixString(16);
print(hexString);
}
});
},
),
TextButton(
style: TextButton.styleFrom(
primary: Colors.blue,
),
onPressed: () async {
// You can get the entered text using the text controller
String enteredText = _textController.text;
print(enteredText);
print(hexString);
// Send the text to the API using the http package
final response = await http.get(Uri.parse(
"http://10.10.10.254/httpapi.asp?command=wlanConnectApEx:ssid=$ssid:ch=1:auth=$auth:encry=AES:pwd=$enteredText:chext=1"),
);
if (response.statusCode == 200) {
Navigator.pushNamed(context, '/');
} else {
// There was an error with the request
// You can handle the error here
}
},
child: Text("Connect"),
),
],
),
),
);
}
}
CodePudding user response:
The problem is that you have defined hexString
within your build
method, so every setState
it gets reset:
Widget build(BuildContext context) {
final args = ModalRoute.of(context)?.settings?.arguments as List<String>;
...
String hexString = '';
Instead, define hexString
in your _state
class:
class _MyHomePageState extends State<MyHomePage> {
final _textController = TextEditingController();
final String title = '';
final String subtitle = '';
hexString = ''; // <-- Define it here
CodePudding user response:
The reason that happened is that you define hexString
and other variable inside build
method, and every time you call setState
, it reset hexString
and set empty value to it, just define those variable out of build method and pass its value in initState, like this:
List<String> args = [];
String ssid = "";
String auth = "";
String hexString = '';
@override
void initState() {
super.initState();
args = ModalRoute.of(context)?.settings?.arguments as List<String>;
ssid = args[0];
auth = args[1];
}
@override
Widget build(BuildContext context) {
return Scaffold(
...
);
}