Reputation: 4921
I am trying to create a variable in onPressed
. But it says error: expected {
. Can someone show an example of onPressed where a variable is initialized, declared and used?
Here is my code:
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
class TestingWidget extends StatefulWidget {
@override
_TestingWidgetState createState() => _TestingWidgetState();
}
var requestbody = {
"apiKey":"z_P!EJ?dsverbtbspV3K4GxaET",
"operationType":"register_user_otp",
"operationData": {
"email": "[email protected]",
"userName":"dvre",
"accessKey":"2defr",
"accessValue":"32rfg43v5",
"registrationTimeStamp": 1616919213853
}
};
var url = Uri.parse('https://script.google.com/macros/s/AKfycbz7kTROol8u509M_p9pMZ9XRnL-myVjcRQKeb9Etp_OIMPnH640vHf_0Jp2dvRIco7kOg/exec');
class _TestingWidgetState extends State<TestingWidget> {
@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: () => {
var response = await http.post(url, body: requestbody);
print('Response status: ${response.statusCode}');
print('Response body: ${response.body}');
},
child: new Text('Click me'),
);
}
}
Upvotes: 1
Views: 746
Reputation: 4249
For anonymous functions use =>
or {}
not both. https://dart.dev/guides/language/language-tour#anonymous-functions
Upvotes: 2