Anonymous
Необработанное исключение: не удалось выполнить поиск хоста: «https:myexampleapi.com»
Сообщение
Anonymous » 24 апр 2024, 03:54
У меня есть API, который предоставляет мне данные каждый раз, когда я отправляю запрос
POST с двумя ключами:
u и
pw .
Структура данных API выглядит следующим образом:
Кроме того, это
Виджет с отслеживанием состояния , который я создал.
Это кнопка с некоторыми настраиваемыми атрибутами.
Код: Выделить всё
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 createState() => _LigoButtonState();
}
class _LigoButtonState extends State {
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(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,
),
),
),
),
),
);
}
}
Я использовал свой виджет в основном коде и отправляю запрос
POST , как показано ниже:
Код: Выделить всё
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: {
"Content-Type": "application/json",
},
body: jsonEncode({
'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,
),
),
),
);
},
);
},
),
),
И каждый раз, когда я нажимаю на виджет
LigoButton , появляется эта ошибка.
Код: Выделить всё
E/flutter ( 6068): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Failed host lookup: 'myexampleapi.com'
Буду признателен, если вы дадите мне ответ или что-нибудь, что может мне в этом помочь.
Подробнее здесь:
https://stackoverflow.com/questions/783 ... pleapi-com
1713920055
Anonymous
У меня есть API, который предоставляет мне данные каждый раз, когда я отправляю запрос [b]POST[/b] с двумя ключами: [b]u[/b] и [b]pw[/b]. Структура данных API выглядит следующим образом: [code]{ "status": "done", "coins": 123456, } [/code] Кроме того, это [b]Виджет с отслеживанием состояния[/b], который я создал. Это кнопка с некоторыми настраиваемыми атрибутами. [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 createState() => _LigoButtonState(); } class _LigoButtonState extends State { 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(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] Я использовал свой виджет в основном коде и отправляю запрос [b]POST[/b], как показано ниже: [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: { "Content-Type": "application/json", }, body: jsonEncode({ '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] И каждый раз, когда я нажимаю на виджет [b]LigoButton[/b], появляется эта ошибка. [code]E/flutter ( 6068): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Failed host lookup: 'myexampleapi.com' [/code] Буду признателен, если вы дадите мне ответ или что-нибудь, что может мне в этом помочь. Подробнее здесь: [url]https://stackoverflow.com/questions/78375533/unhandled-exception-failed-host-lookup-httpsmyexampleapi-com[/url]