I get a socket exception err when I call the listDocuments() method.
Here's the code I use in Flutter to query:
Future<List<Document>> getNotes() async {
DocumentList allNotes = await appwriteDatabase.listDocuments(
databaseId: dojoDatabaseId,
collectionId: noteCollectionId,
queries: [
Query.select([
'subject',
'description',
'date',
'id',
//Permission is tijdelijk vanwege een bug in de SDK
'\$permissions'
]),
Query.orderDesc('date'),
],
);
return allNotes.documents;
}```
Strangely enough when I turn off catch all exceptions in VS code; It ignores the exception. When I press a button which calls the method a second time it works 🫣 . Any ideas?
I tried the solution mentioned here: https://discord.com/channels/564160730845151244/1166669095639466074
How are you initializing appwriteDatabase?
This is the implementation:
// init the client
void _initClient() {
appwriteClient
.setEndpoint(apiEndpoint)
.setProject(projectId)
.setSelfSigned(status: true);
}```
And I use the main method's initState to init appwrite:
void main() {
runApp(
const Yushinkan(),
);
}
class Yushinkan extends StatefulWidget {
const Yushinkan({super.key});
@override
State<Yushinkan> createState() => _YushinkanState();
}
class _YushinkanState extends State<Yushinkan> {
final AppwriteService _appwriteService = AppwriteService();
@override
void initState() {
//Initializing Appwrite
super.initState();
_appwriteService.initAppwrite();
}```
What's the rest of the code?
import 'package:appwrite/appwrite.dart';
import 'package:appwrite/models.dart';
class AppwriteService {
static String projectId = '***';
static String apiEndpoint = 'https://appwrite.samrahim.nl/v1';
static String dojoDatabaseId = 'dojodb';
static String noteCollectionId = 'notes';
static Client appwriteClient = Client();
static final Account _appwriteAccount = Account(appwriteClient);
late Databases appwriteDatabase;
AppwriteService._();
static final AppwriteService _appwriteService = AppwriteService._();
factory AppwriteService() => _appwriteService;
// init the client
void _initClient() {
appwriteClient
.setEndpoint(apiEndpoint)
.setProject(projectId)
.setSelfSigned(status: true);
}
// init the databases using client
void _initDatabase(client) {
appwriteDatabase = Databases(client);
}
// Initialize
void initAppwrite() {
_initClient();
_initDatabase(appwriteClient);
}
Future<List<Document>> getNotes() async {
DocumentList allNotes = await appwriteDatabase.listDocuments(
databaseId: dojoDatabaseId,
collectionId: noteCollectionId,
queries: [
Query.select([
'subject',
'description',
'date',
'id',
//Permission is tijdelijk vanwege een bug in de SDK
'\$permissions'
]),
Query.orderDesc('date'),
],
);
return allNotes.documents;
}
void createNote(Map<dynamic, dynamic> noteData, String noteId) {
try {
appwriteDatabase.createDocument(
databaseId: dojoDatabaseId,
collectionId: noteCollectionId,
documentId: noteId,
data: noteData);
} on AppwriteException catch (e) {
print(e);
}
}
Here you go
Do you have a valid certificate?
Maybe you can test making a http request to Google to confirm you have networking enabled
Is that necessary? Because in runtime certain exceptions are handled by the Flutter framework and eventually still connects to the DB. The only difference is that sometimes, even though I see in the dashboard that the list of documents is updated, it still returns the previous state of the listDocuments. As you can see in the code here I print whatever listDocuments() returns.
Because it connects to the DB eventually I think we can rule out network permissions right?
Future<List<TrainingNote>> downloadTrainingNotes() async {
const int maxRetries = 3;
int retryCount = 0;
while (retryCount < maxRetries) {
try {
List<Document> notesFromTheCloud = await _appwriteService
.getNotes()
.timeout(const Duration(seconds: 10));
_trainingNotes.clear();
for (Document note in notesFromTheCloud) {
//For debug purposes
print('The subject was: ${note.data['subject']}');
TrainingNote trainingNote = TrainingNote.fromJsonToObject(note.data);
_trainingNotes.add(trainingNote);
}
return _trainingNotes;
} on TimeoutException catch (e) {
print("Timeout bij het ophalen van notities: $e");
retryCount++;
print("Opnieuw proberen poging $retryCount...");
Future.delayed(const Duration(seconds: 2));
} catch (error) {
print('Fout bij het ophalen van notities: $error');
return <TrainingNote>[];
}
}
print(
"Maximale pogingen bereikt, kon notities niet ophalen na $maxRetries pogingen");
return <TrainingNote>[];
}```
I might know whats going on. I think I should be using RealTime for this feature instead of async Futures in Dart. But then again, it shouldn't give an exception.
As you suggested I've manually put the permissions for internet but to no avail.
Hi @Drake, I placed a print statement everytime the listDocument method gets executed. I noticed something strange happening. This is my code:
DocumentList allNotes = await appwriteDatabase.listDocuments(
databaseId: dojoDatabaseId,
collectionId: noteCollectionId,
queries: [
Query.select([
'subject',
'description',
'date',
'id',
//Temp because of a bug in the sdk
'\$permissions'
]),
Query.orderDesc('date'),
],
);
print(allNotes.documents.length);
return allNotes.documents;
}```
At the end you can see that I have a print statement which tells me this amount of documents in the DB. However I see in the DB that there are 6 documents. The print statement shows I got 5 documents. Is this an issue you've read somewhere before?
Okay it's solved now. It was due to my programming skills honestly. The addNote/createNote method that uses the createDocument method of the SDK wasn't async. I've added the Future<void> and now everything is peachy. Leaving this here for other devs to read my silly mistake.
[Solved] Socket exception
Recommended threads
- Apple OAuth Scopes
Hi Hi, I've configured sign in with apple and this is the response i'm getting from apple once i've signed in. I cant find anywhere I set scopes. I remember se...
- Sign In With Apple OAuth Help
Hi All! I've got a flutter & appwrite app which Im trying to use sign in with apple for. I already have sign in with google working and the function is the sam...
- Type Mismatch in AppwriteException
There is a discrepancy in the TypeScript type definitions for AppwriteException. The response property is defined as a string in the type definitions, but in pr...