I am getting an error Code 2002, while trying to register a user on backendless. I have checked my initApp file and my Application ID is working, I am on the trail account and doing the challenges to unlock springboard.
I am using a Forms for user input, Provider to manage my state and Backendless for my data.
My Register page is as follows:
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../../viewmodels/usermanage_provider.dart';
import '../../viewmodels/usermanhelper.dart';
// ignore: must_be_immutable
class RegistrationScreen extends StatelessWidget {
static const routeName = '/registration';
final GlobalKey<FormState> _formKey = GlobalKey();
late String _email;
late String _password;
late String _username;
// final TextEditingController _emailController = TextEditingController();
// final TextEditingController _passwordController = TextEditingController();
// final TextEditingController _nameController = TextEditingController();
RegistrationScreen({super.key});
@override
Widget build(BuildContext context) {
return Consumer<UserManagementViewModel>(
builder: (context, value, child) {
return Scaffold(
body: SingleChildScrollView(
child: Column(
children: [
Form(
key: _formKey,
child: Padding(
padding: const EdgeInsets.all(10),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Padding(
padding: EdgeInsets.all(30.0),
child: Text(
'Register User',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 46,
fontWeight: FontWeight.w200,
color: Colors.white),
),
),
TextFormField(
//controller: _emailController,
keyboardType: TextInputType.emailAddress,
decoration: const InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide: BorderSide.none,
borderRadius: BorderRadius.all(
Radius.circular(4.0),
),
),
labelText: 'Enter your email address',
fillColor: Colors.white,
filled: true,
),
validator: (value) {
if (value!.isEmpty) {
return 'Please enter your email';
} else if (!isValidEmail(value)) {
return 'Please enter Valid email address';
}
return null;
},
onSaved: (value) {
_email = value!;
},
),
const SizedBox(
height: 16,
),
TextFormField(
obscureText: true,
decoration: const InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide: BorderSide.none,
borderRadius: BorderRadius.all(
Radius.circular(4.0),
),
),
labelText: 'Password',
fillColor: Colors.white,
filled: true,
),
validator: (value) {
if (value!.isEmpty) {
return 'Please enter your password';
}
return null;
},
onSaved: (value) {
_password = value!;
},
),
const SizedBox(
height: 16,
),
TextFormField(
decoration: const InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide: BorderSide.none,
borderRadius: BorderRadius.all(
Radius.circular(4.0),
),
),
labelText: 'Enter your Username',
fillColor: Colors.white,
filled: true,
),
validator: (value) {
if (value!.isEmpty) {
return 'Please enter your username';
}
return null;
},
onSaved: (value) {
_username = value!;
},
),
const SizedBox(
height: 16,
),
if (value.isLoading)
const LinearProgressIndicator()
else
ElevatedButton(
onPressed: () {
if (_formKey.currentState!.validate()) {
_formKey.currentState!.save();
final userViewModel =
Provider.of<UserManagementViewModel>(
context,
listen: false);
createNewUserInUI(context,
email: _email,
password: _password,
username: _username);
// createNewUserInUI(
// context,
// email: _emailController.text.trim(),
// password: _passwordController.text.trim(),
// username: _nameController.text.trim(),
// );
// print('typed');
}
},
child: const Text('Register')),
// if (viewModel.isRegistered)
// const Text('User Registered Successfully')
// else if (viewModel.isRegistered != null &&
//// !viewModel.isRegistered)
// const Text(('User is already registered. '))
],
),
),
),
],
),
),
);
},
);
}
bool isValidEmail(String value) {
// Email validation regex
final regex = RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$');
return regex.hasMatch(value);
}
}
This code is my User Management logic
import 'package:backendless_sdk/backendless_sdk.dart';
import 'package:flutter/material.dart';
import 'package:unitapp/models/unit_entry.dart';
class UserManagementViewModel with ChangeNotifier {
BackendlessUser? _currentUser;
BackendlessUser? get curreentUser => _currentUser;
void setCurrentUserNull() {
_currentUser = null;
}
bool _isRegistered = false;
bool get isRegisered => _isRegistered;
set isRegister(bool value) {
_isRegistered = value;
notifyListeners();
}
bool _isloading = false;
String _userProgressText = '';
String get userProgressText => _userProgressText;
// User get user => _user;
bool get isRegistered => _isRegistered;
bool get isLoading => _isloading;
Future<String> registerUser(BackendlessUser user) async {
String result = 'OK';
_userProgressText = 'Registering you as a new user... Please wait. ';
_isloading = true;
notifyListeners();
try {
await Backendless.userService.register(user);
UnitEntry emptyEntry = UnitEntry(units: {}, username: user.email);
await Backendless.data
.of('UnitEntry')
.save(emptyEntry.toJson())
.onError((error, stackTrace) {
result = error.toString();
});
} on BackendlessException catch (e) {
result = e.toString();
} finally {
_isloading = false;
notifyListeners();
}
return result;
}
void checkIfRegistered(String username) async {
DataQueryBuilder queryBuilder = DataQueryBuilder()
..whereClause = "email = '$username'";
await Backendless.data
.withClass<BackendlessUser>()
.find(queryBuilder)
.then((value) {
if (value == null || value.length == 0) {
_isRegistered = false;
notifyListeners();
} else {
_isRegistered = true;
notifyListeners();
}
}).onError(
(error, stackTrace) {
print(error.runtimeType);
},
);
}
}
Lastly the code to help create the user in the UI
import 'package:backendless_sdk/backendless_sdk.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:unitapp/viewmodels/usermanage_provider.dart';
void createNewUserInUI(BuildContext context,
{required String email,
required String password,
required String username}) async {
FocusManager.instance.primaryFocus?.unfocus();
if (email.isEmpty || password.isEmpty || username.isEmpty) {
SnackBar(content: Text('Enter all fields'));
} else {
BackendlessUser user = BackendlessUser()
..email = email.trim()
..password = password.trim()
..putProperties(
{
'username': username.trim(),
},
);
String result =
await context.read<UserManagementViewModel>().registerUser(user);
if (result != 'OK') {
SnackBar(
content: Text('Successfully Registered'),
showCloseIcon: true,
);
} else {
const SnackBar(
showCloseIcon: true,
content: Text('User not registered nigga'),
);
}
}
}