我是 Flutter 的新手,我為 mysql 中的插入和用戶制作了一個注冊表。如果注冊成功與否,我正在使用 FlutterToast 的“未來異步”功能顯示文本,這與我的 register.php 存檔相關聯。我需要顯示文本或顯示警報訊息(如何在 FlutterToast 中放置警報?我嘗試了“msg”而不是“text”,但沒有用)。
我的功能:
Future register() async {
var url =
"http://192.167.1.142/databasedb/register.php";
var response = await http.post(url, body: {
"correo": correo.text,
"celular": celular.text,
"passwd": passwd.text,
"passwd2": passwd2.text
});
var data = json.decode(response.body);
if (data == "Error") {
FlutterToast(context).showToast(
child: Text(
'User invalid',
style: TextStyle(fontSize: 25, color: Colors.red),
));
} else {
FlutterToast(context).showToast(
child: Text('Registration Successful',
style: TextStyle(fontSize: 25, color: Colors.green)));
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DashBoard(),
),
);
}
}
我的提交按鈕:
Expanded(
child: ElevatedButton(
style: ElevatedButton.styleFrom(
onSurface: Colors.blue,
),
child: Text('Register',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.white)),
onPressed: () => [
// enabled: isButtonActive,
if (isButtonActive)
{
setState(() => isButtonActive = false),
}
else
{null},
register(), //REGISTER FUNCTION
setState(() {})
],
),
),
我的注冊.php
<?php
$db = mysqli_connect('localhost','root','','databasedb');
//Conectar al hosting
if (!$db) {
echo "Database connection faild";
}
$cell= $_POST['celular'];
$email= $_POST['correo'];
$pass= $_POST['passwd'];
$pass2= $_POST['passwd2'];
$sql = "SELECT celular FROM usuario WHERE celular = '".$cell."' AND passwd = '".$pass."'";
$result = mysqli_query($db,$sql);
$count = mysqli_num_rows($result);
if ($count == 1) {
echo json_encode("Error");
}else{
if($pass == $pass2 && strlen($pass) >= 8 && strlen($email) >= 10 && (strlen($cell) >= 9 && strlen($cell) <= 15)){
$insert = "INSERT INTO usuario(correo,celular,passwd,passwd2)VALUES('".$email."','".$cell."','".$pass."','".$pass2."')";
$query = mysqli_query($db,$insert);
if ($query) {
echo json_encode("Success");
}
}else{
echo json_encode("Contrase?a inválida");
}
}
?>
當我在 Chrome 中運行 flutter 并提交時,控制臺中會出現它:
engine.dart:457 [Violation] 'requestAnimationFrame' handler took 51ms
:55843/#/:1 Access to XMLHttpRequest at 'http://192.167.1.142/databasedb/register.php' from origin 'http://localhost:55843' has been blocked by CORS policy: No 'Access to XMLHttpRequest at http://192.167.1.142/databasedb/register.php from :55843/·/:! origin http://localhost:55843 has been blocked by
CORS policy: No 'Access-Control-Alow-Origin' header is present on the requested resource.
errors.dart:202 Uncaught (in promise) Error: XMLHttpRequest error.
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart 906:28 get current
packages/http/src/browser_client.dart 84:22 <fn>
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/zone.dart 1687:54 runUnary
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 160:18 handleValue
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 767:44 handleValueCallback
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 796:13 _propagateToListeners
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 593:7 [_complete]
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/stream_pipe.dart 61:11 _cancelAndValue
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/stream.dart 1288:7 <fn>
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 334:14 _checkAndCall
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 339:39 dcall
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/html/dart2js/html_dart2js.dart 37254:58 <fn>
at Object.createErrorWithStack (errors.dart:284)
at Object._rethrow (async_patch.dart:200)
at async._AsyncCallbackEntry.new.callback (zone.dart:1413)
at Object._microtaskLoop (schedule_microtask.dart:40)
at _startMicrotaskLoop (schedule_microtask.dart:49)
at async_patch.dart:166
DartError @ errors.dart:202
RethrownDartError @ errors.dart:240
createErrorWithStack @ errors.dart:284
_rethrow @ async_patch.dart:200
(anonymous) @ zone.dart:1413
_microtaskLoop @ schedule_microtask.dart:40
_startMicrotaskLoop @ schedule_microtask.dart:49
(anonymous) @ async_patch.dart:166
Promise.then (async)
_scheduleImmediateWithPromise @ async_patch.dart:164
_scheduleImmediate @ async_patch.dart:136
_scheduleAsyncCallback @ schedule_microtask.dart:69
_rootScheduleMicrotask @ zone.dart:1495
scheduleMicrotask @ zone.dart:1707
[_asyncCompleteError] @ future_impl.dart:665
[_completeError] @ future_impl.dart:50
completeError @ future_impl.dart:33
(anonymous) @ browser_client.dart:82
runUnary @ zone.dart:1687
handleValue @ future_impl.dart:160
handleValueCallback @ future_impl.dart:767
_propagateToListeners @ future_impl.dart:796
[_complete] @ future_impl.dart:593
_cancelAndValue @ stream_pipe.dart:61
(anonymous) @ stream.dart:1288
_checkAndCall @ operations.dart:334
dcall @ operations.dart:339
(anonymous) @ html_dart2js.dart:37254
error (async)
[_addEventListener] @ html_dart2js.dart:15814
[dartx.addEventListener] @ html_dart2js.dart:15802
[_tryResume] @ html_dart2js.dart:37284
onData @ html_dart2js.dart:37255
get first @ stream.dart:1287
send @ browser_client.dart:79
(anonymous) @ async_patch.dart:45
runUnary @ zone.dart:1687
handleValue @ future_impl.dart:160
handleValueCallback @ future_impl.dart:767
_propagateToListeners @ future_impl.dart:796
[_completeWithValue] @ future_impl.dart:602
(anonymous) @ future_impl.dart:640
_microtaskLoop @ schedule_microtask.dart:40
_startMicrotaskLoop @ schedule_microtask.dart:49
(anonymous) @ async_patch.dart:166
Promise.then (async)
_scheduleImmediateWithPromise @ async_patch.dart:164
_scheduleImmediate @ async_patch.dart:136
_scheduleAsyncCallback @ schedule_microtask.dart:69
_rootScheduleMicrotask @ zone.dart:1495
scheduleMicrotask @ zone.dart:1707
[_asyncCompleteWithValue] @ future_impl.dart:639
[_asyncComplete] @ future_impl.dart:634
_Future.immediate @ future_impl.dart:283
value @ future.dart:334
forTap @ feedback.dart:107
runBody @ async_patch.dart:84
_async @ async_patch.dart:123
forTap @ feedback.dart:97
[_handleTap] @ ink_well.dart:988
invokeCallback @ recognizer.dart:198
handleTapUp @ tap.dart:608
[_checkUp] @ tap.dart:296
handlePrimaryPointer @ tap.dart:230
handleEvent @ recognizer.dart:563
[_dispatch] @ pointer_router.dart:94
(anonymous) @ pointer_router.dart:139
forEach @ linked_hash_map.dart:21
[_dispatchEventToRoutes] @ pointer_router.dart:137
route @ pointer_router.dart:123
handleEvent @ binding.dart:439
dispatchEvent @ binding.dart:419
dispatchEvent @ binding.dart:322
[_handlePointerEventImmediately] @ binding.dart:374
handlePointerEvent @ binding.dart:338
[_flushPointerEventQueue] @ binding.dart:296
[_handlePointerDataPacket] @ binding.dart:279
invoke1 @ platform_dispatcher.dart:1020
invokeOnPointerDataPacket @ platform_dispatcher.dart:184
[_onPointerData] @ pointer_binding.dart:130
(anonymous) @ pointer_binding.dart:558
(anonymous) @ pointer_binding.dart:511
loggedHandler @ pointer_binding.dart:217
在我看來,文本根本沒有顯示,但已成功提交到資料庫中
請幫幫我,謝謝。
uj5u.com熱心網友回復:
看起來好像您從服務器收到 CORS 錯誤。嘗試添加到您的回應中(在 PHP 中):
header('Access-Control-Allow-Origin: *');
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/397906.html
上一篇:使用本地WSDL發送到遠程URL
