I'm new in Flutter and I make an register form for insert and user in mysql. I'm using a "Future async" function with FlutterToast for show text if the registration is successfull or not and this is connected with my register.php archive. I need show the text or show an alert message (How do I put an alert inside the FlutterToast? I tried "msg" instead of "text" but it didn't work).
My function:
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(),
),
);
}
}
My submit button:
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(() {})
],
),
),
My register.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");
}
}
?>
When I run flutter and submit in Chrome, in console appears it:
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
The text isn't showing at all in my view, but submit successfully into database
Please help me, thanks.
CodePudding user response:
Looks as though you are getting a CORS error from your server. Try adding the to your response (in PHP):
header('Access-Control-Allow-Origin: *');