I have an API that gives me data each time I send POST request with 2 keys: u & pw.
The Data Structure of API provides is like this below:
<code>{
"status": "done",
"coins": 123456,
}
</code>
<code>{
"status": "done",
"coins": 123456,
}
</code>
{
"status": "done",
"coins": 123456,
}
Also this is an Stateful Widget I Created.
It’s a button with some customized Attributes.
<code>class LigoButton extends StatefulWidget {
final String text;
final double width;
final double height;
final VoidCallback ontap;
const LigoButton({
super.key,
required this.text,
required this.width,
required this.height,
required this.ontap,
});
@override
State<LigoButton> createState() => _LigoButtonState();
}
class _LigoButtonState extends State<LigoButton> {
bool isLoading = false;
@override
Widget build(BuildContext context) {
return Container(
width: widget.width,
height: widget.height,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30.0),
gradient: const LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: <Color>[
Color(0xffFEAC5D),
Color(0xffF43469),
],
),
),
child: Material(
color: Colors.transparent,
borderRadius: BorderRadius.circular(30.0),
child: InkWell(
onTap: () async {
if (isLoading) return;
setState(() {
isLoading = true;
});
widget.ontap();
await Future.delayed(const Duration(seconds: 1));
setState(() {
isLoading = false;
});
},
borderRadius: BorderRadius.circular(30.0),
child: Center(
child: isLoading
? const SizedBox(
width: 20.0,
height: 20.0,
child: CircularProgressIndicator(
color: Colors.white,
),
)
: Text(
widget.text.tr,
style: const TextStyle(
color: Colors.white,
fontSize: 17.0,
fontFamily: 'Lalezar',
fontWeight: FontWeight.normal,
),
),
),
),
),
);
}
}
</code>
<code>class LigoButton extends StatefulWidget {
final String text;
final double width;
final double height;
final VoidCallback ontap;
const LigoButton({
super.key,
required this.text,
required this.width,
required this.height,
required this.ontap,
});
@override
State<LigoButton> createState() => _LigoButtonState();
}
class _LigoButtonState extends State<LigoButton> {
bool isLoading = false;
@override
Widget build(BuildContext context) {
return Container(
width: widget.width,
height: widget.height,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30.0),
gradient: const LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: <Color>[
Color(0xffFEAC5D),
Color(0xffF43469),
],
),
),
child: Material(
color: Colors.transparent,
borderRadius: BorderRadius.circular(30.0),
child: InkWell(
onTap: () async {
if (isLoading) return;
setState(() {
isLoading = true;
});
widget.ontap();
await Future.delayed(const Duration(seconds: 1));
setState(() {
isLoading = false;
});
},
borderRadius: BorderRadius.circular(30.0),
child: Center(
child: isLoading
? const SizedBox(
width: 20.0,
height: 20.0,
child: CircularProgressIndicator(
color: Colors.white,
),
)
: Text(
widget.text.tr,
style: const TextStyle(
color: Colors.white,
fontSize: 17.0,
fontFamily: 'Lalezar',
fontWeight: FontWeight.normal,
),
),
),
),
),
);
}
}
</code>
class LigoButton extends StatefulWidget {
final String text;
final double width;
final double height;
final VoidCallback ontap;
const LigoButton({
super.key,
required this.text,
required this.width,
required this.height,
required this.ontap,
});
@override
State<LigoButton> createState() => _LigoButtonState();
}
class _LigoButtonState extends State<LigoButton> {
bool isLoading = false;
@override
Widget build(BuildContext context) {
return Container(
width: widget.width,
height: widget.height,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30.0),
gradient: const LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: <Color>[
Color(0xffFEAC5D),
Color(0xffF43469),
],
),
),
child: Material(
color: Colors.transparent,
borderRadius: BorderRadius.circular(30.0),
child: InkWell(
onTap: () async {
if (isLoading) return;
setState(() {
isLoading = true;
});
widget.ontap();
await Future.delayed(const Duration(seconds: 1));
setState(() {
isLoading = false;
});
},
borderRadius: BorderRadius.circular(30.0),
child: Center(
child: isLoading
? const SizedBox(
width: 20.0,
height: 20.0,
child: CircularProgressIndicator(
color: Colors.white,
),
)
: Text(
widget.text.tr,
style: const TextStyle(
color: Colors.white,
fontSize: 17.0,
fontFamily: 'Lalezar',
fontWeight: FontWeight.normal,
),
),
),
),
),
);
}
}
I used my Widget on the main Code and I’m sending the POST request like this below:
<code>LigoButton(
text: 'checkDepository',
width: 120.0,
height: 50.0,
ontap: () async {
var adminID = adminIDController.text;
var adminPassword = adminPasswordController.text;
final response = await http.post(
Uri.parse("https://myexampleapi.com"),
headers: <String, String>{
"Content-Type": "application/json",
},
body: jsonEncode(<String, String>{
'u': adminID,
'pw': adminPassword,
}),
);
var coins = jsonDecode(response.body);
LigoStatus.status = response.statusCode == StatusCode.OK
? 'OK'
: 'ERROR';
setState(
() {
totalCoinContainer = Container(
width: 200.0,
height: 50.0,
decoration: BoxDecoration(
color: Colors.transparent,
border: Border.all(
color: Colors.green,
width: 1.5,
),
borderRadius: BorderRadius.circular(30.0),
),
child: Center(
child: Text(
coins['coins'],
style: const TextStyle(
color: Colors.green,
fontSize: 18.0,
fontFamily: 'Lalezar',
fontWeight: FontWeight.normal,
),
),
),
);
},
);
},
),
),
</code>
<code>LigoButton(
text: 'checkDepository',
width: 120.0,
height: 50.0,
ontap: () async {
var adminID = adminIDController.text;
var adminPassword = adminPasswordController.text;
final response = await http.post(
Uri.parse("https://myexampleapi.com"),
headers: <String, String>{
"Content-Type": "application/json",
},
body: jsonEncode(<String, String>{
'u': adminID,
'pw': adminPassword,
}),
);
var coins = jsonDecode(response.body);
LigoStatus.status = response.statusCode == StatusCode.OK
? 'OK'
: 'ERROR';
setState(
() {
totalCoinContainer = Container(
width: 200.0,
height: 50.0,
decoration: BoxDecoration(
color: Colors.transparent,
border: Border.all(
color: Colors.green,
width: 1.5,
),
borderRadius: BorderRadius.circular(30.0),
),
child: Center(
child: Text(
coins['coins'],
style: const TextStyle(
color: Colors.green,
fontSize: 18.0,
fontFamily: 'Lalezar',
fontWeight: FontWeight.normal,
),
),
),
);
},
);
},
),
),
</code>
LigoButton(
text: 'checkDepository',
width: 120.0,
height: 50.0,
ontap: () async {
var adminID = adminIDController.text;
var adminPassword = adminPasswordController.text;
final response = await http.post(
Uri.parse("https://myexampleapi.com"),
headers: <String, String>{
"Content-Type": "application/json",
},
body: jsonEncode(<String, String>{
'u': adminID,
'pw': adminPassword,
}),
);
var coins = jsonDecode(response.body);
LigoStatus.status = response.statusCode == StatusCode.OK
? 'OK'
: 'ERROR';
setState(
() {
totalCoinContainer = Container(
width: 200.0,
height: 50.0,
decoration: BoxDecoration(
color: Colors.transparent,
border: Border.all(
color: Colors.green,
width: 1.5,
),
borderRadius: BorderRadius.circular(30.0),
),
child: Center(
child: Text(
coins['coins'],
style: const TextStyle(
color: Colors.green,
fontSize: 18.0,
fontFamily: 'Lalezar',
fontWeight: FontWeight.normal,
),
),
),
);
},
);
},
),
),
And everytime I click on my LigoButton Widget it gives me this Error.
<code>E/flutter ( 6068): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Failed host lookup: 'myexampleapi.com'
</code>
<code>E/flutter ( 6068): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Failed host lookup: 'myexampleapi.com'
</code>
E/flutter ( 6068): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Failed host lookup: 'myexampleapi.com'
I would be thankful if you provide me an answer or sth that can help me with this.
1