```dart
import 'dart:convert';
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';
// Function to retrieve user information from the database
Future<Map<String, dynamic>> getUserInfo(int userId) async {
// Open the database
final databasePath = await getDatabasesPath();
final database = await openDatabase(
join(databasePath, 'your_database_name.db'),
);
try {
// Query the database for user information
final List<Map<String, dynamic>> maps = await database.query(
'users', // Replace with your table name
where: 'id = ?', // Assuming 'id' is the primary key
whereArgs: [userId],
);
// Check if the user exists
if (maps.isNotEmpty) {
// Return the user information as a JSON object
return maps.first;
} else {
// If no user is found, return an empty map or throw an exception
throw Exception('User not found');
}
} catch (e) {
// Handle any errors that occur during the database operation
print('Error retrieving user information: $e');
rethrow;
} finally {
// Close the database connection
await database.close();
}
}
// Example usage
void main() async {
try {
final userInfo = await getUserInfo(1); // Replace with the desired user ID
print(jsonEncode(userInfo)); // Print the user information as a JSON string
} catch (e) {
print('Failed to retrieve user information: $e');
}
}
```