I get this weird problem in Flutter. I’m using ‘Provider’ for state management, In the helper class everything is fine, I’m sending the request and I receive the response with no problems. The problem occurs in the button event handler. The first time I click the button literally nothing happens, when I click it the second time it talks to the API and gets the data. Another problem is: when I enter correct data and they are sent to the API, Immediately after that, I enter wrong credentials the app tells me that I’ve entered correct credentials as if I’m dealing with the same previous object.
Here is my code for the front-end part:
textDirection: TextDirection.rtl,
child: Consumer<PicoProvider>(
builder: (context, prov, child) => SafeArea(
child: Scaffold(
backgroundColor: CustomColors.scaffoldDark,
body: Padding(
padding: const EdgeInsets.all(100),
child: SingleChildScrollView(
child: Container(
width: screenWidth,
child: Form(
autovalidateMode: AutovalidateMode.onUserInteraction,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
decoration: BoxDecoration(
color: Colors.black,
borderRadius: BorderRadius.circular(10),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.5),
spreadRadius: 5,
blurRadius: 7,
offset: const Offset(0, 3),
),
]),
child: Image.asset(
'assets/images/main_logo.jpeg',
height: 300,
),
),
const SizedBox(height: 20),
CTextField(
label: 'اسم المسخدم',
icon: Ionicons.person,
isObsecured: false,
controller: userNameController),
const SizedBox(height: 20),
CTextField(
label: 'كلمة المرور',
icon: Ionicons.lock_closed,
isObsecured: true,
controller: passwordController,
onSubmitted: () {
prov.websiteLogin(userNameController.text,
passwordController.text);
}),
ElevatedButton(
onPressed: () {
prov.websiteLogin(userNameController.text,
passwordController.text);
prov.errorText!.isNotEmpty
? giveMeDialog(
context, 'hello', 'not found', 'error')
: giveMeDialog(context, 'hello',
'you are logged', 'success');
},
child: const Text('Click')),
const SizedBox(height: 20),
prov.isLoading
? const CircularProgressIndicator()
: const SizedBox(),
Text('${prov.errorText}')
],
),
),
),
),
),
),
),
),
);
Here is my code for the back-end part:
class PicoProvider extends ChangeNotifier {
String baseURL = 'http://111.11.11.111:1111';
bool _isLoading = false;
bool get isLoading => _isLoading;
User? _user = User();
User? get user => _user;
String? _errorText;
String? get errorText => _errorText;
Future<void> websiteLogin(String userName, String password) async {
try {
String endPoint =
'$baseURL/PicoLogin?UserName=$userName&Password=$password';
_isLoading = true;
notifyListeners();
var response = await get(Uri.parse(endPoint), headers: {
"content-type": "application/json",
"Access-Control-Allow-Origin": "*",
});
if (response.statusCode == 200) {
_user = User.fromJson(jsonDecode(response.body));
_errorText = '';
} else {
_errorText = 'User Not Found';
}
_isLoading = false;
notifyListeners();
} catch (e) {
_errorText = e.toString();
notifyListeners();
print('Error: ${e.toString()}');
}
}
}