I am trying to implement a basic login function using Appwrite in Flutter, and no matter what way I have done this I keep getting a Concurrent modification error rather than the future returning properly with the account session, but it's inconsistent and seemingly random if it fails or not. I can not however determine why this is happening, as I am not using the data returned in the future in any way. I'm simply triggering a callback to navigate to a new page. Any help in figuring this out if it's me or not would be appreciated.
The expected result is that on pressing the login button, the Appwrite future runs inside the method call and then once it completes, then the application will trigger the move to the dashboard. It doesn't save, use or in anyway access this information at this time This does happen, when it happens, without issue. But it needs to happen 100% of the time, not randomly like it is currently.
This is the error that is caught and thrown randomly:
flutter: AppwriteException: null, Concurrent modification during iteration: Instance(length:2) of '_GrowableList'. (0)
This isn't the only Appwrite method throwing this error either, but part of my troubleshooting has been to take the login that I'm building and boil it down to the absolute bare minimum, as you can see in my code below. And yet this still keeps happening. I'm even using almost exactly the code that is listed under their doc samples here.
I have updated both my Flutter version, and the Appwrite package version to the latest releases, and reviewed their bug reports over on their Github page, thinking this was something in their package, but this is EVERYWHERE in my implementation of Appwrite. I would think that this would be a huge problem if it wasn't just something I'm doing wrong or misunderstanding, and not once is it brought up in almost 300 open issues on the project page. I can't find anything specifically related to this issue on here either. There's' the usual information on concurrent modification errors and how they happen, how to avoid them, etc. But I'm not accessing, using, or modifying any data here, at all.
As best I can tell, this shouldn't be an issue with asynchronous calls, because I'm not actually doing anything with the returned data at this point in the code base, nor later. I don't store this in any way shape or form, I haven't made it that far due to this. It's just waiting for the completion of the future, then triggering the Navigation to the dashboard page.
Below is the login method in question:
// Login to Appwrite Backend
login(BuildContext context,String username, String password) {
Client client = Client();
Account account = Account(client);
client
.setEndpoint('https://jeff.vmsimple.com/v1') // Your API Endpoint
.setProject('jeffs-timekeeper'); // Your project ID
Future result = account.createSession(
email: username,
password: password,
);
result.then((response) {
print(response);
// Navigator.push(
// context,
// MaterialPageRoute(
// builder: (context) =>
// const DashboardScreen(title: 'Dashboard')),
// );
Navigator.popAndPushNamed(context, '/dashboardScreen');
// return response;
}).catchError((error) {
print(error);
// return null;
});
}
And below is the flutter build function that uses it for a callback when pressing login. This is a stateless widget, with no moving parts other than the form fields, which get passed into the login method above.
Widget build(BuildContext context) {
// Check If logged in, and redirect to Dashboard if so
// checkLogin(context);
TextEditingController _emailController = TextEditingController();
TextEditingController _passwordController = TextEditingController();
return Scaffold(
appBar: AppBar(
title: Text(title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Login',
style: Theme.of(context).textTheme.headline4,
),
Container(
margin: const EdgeInsets.fromLTRB(25, 25, 25, 12.5),
child: TextField(
controller: _emailController,
obscureText: false,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Username',
),
),
),
Container(
margin: const EdgeInsets.fromLTRB(25, 12.5, 25, 25),
child: TextField(
controller: _passwordController,
obscureText: true,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Password',
),
),
),
Container(
margin: const EdgeInsets.only(bottom: 25),
child: Row(
children: [
Expanded(
child: Column(
children: [
ElevatedButton(
onPressed: () => {
login(context,_emailController.text, _passwordController.text),
// getCurrentSession(),
},
child: const Text('Login'))
],
)),
],
))
],
),
),
);
}
This is the output of Flutter Doctor -v
flutter doctor -v
[√] Flutter (Channel stable, 3.0.1, on Microsoft Windows [Version 10.0.19044.1706], locale en-US)
• Flutter version 3.0.1 at C:\sdk\flutter_windows_2.10.5-stable\flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision fb57da5f94 (3 weeks ago), 2022-05-19 15:50:29 -0700
• Engine revision caaafc5604
• Dart version 2.17.1
• DevTools version 2.12.2
[√] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
• Android SDK at C:\Users\sinne\AppData\Local\Android\sdk
• Platform android-32, build-tools 32.1.0-rc1
• Java binary at:
C:\Users\sinne\AppData\Local\JetBrains\Toolbox\apps\AndroidStudio\ch-0\211.7628.21.2111.8309675\jre\bin\java
• Java version OpenJDK Runtime Environment (build 11.0.11+9-b60-7590822)
• All Android licenses accepted.
[√] Chrome - develop for the web
• Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe
[√] Visual Studio - develop for Windows (Visual Studio Community 2019 16.11.10)
• Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
• Visual Studio Community 2019 version 16.11.32126.315
• Windows 10 SDK version 10.0.22000.0
[√] Android Studio (version 2021.1)
• Android Studio at C:\Users\sinne\AppData\Local\JetBrains\Toolbox\apps\AndroidStudio\ch-0\211.7628.21.2111.8309675
• Flutter plugin version 67.1.1
• Dart plugin version 211.7817
• Java version OpenJDK Runtime Environment (build 11.0.11+9-b60-7590822)
[√] Connected device (3 available)
• Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.19044.1706]
• Chrome (web) • chrome • web-javascript • Google Chrome 102.0.5005.63
• Edge (web) • edge • web-javascript • Microsoft Edge 102.0.1245.30
[√] HTTP Host Availability
• All required HTTP hosts are available
• No issues found!