I am trying to connect my Flutter App to a SQL Server database with using dart plugin mssql_connection
, without using any API.
Can anyone tell me why I am getting this error?
Error connecting to database: MissingPluginException (no implementation found for method connect on channel mssql_connection)
import 'package:mssql_connection/mssql_connection.dart';
class DataHelper {
final MssqlConnection _connection = MssqlConnection.getInstance();
// Method to connect to the database
Future<bool> connectToDatabase() async {
try {
bool isConnected = await _connection.connect(
ip: 'iplink',
port: '1433',
databaseName: 'dataBaseIMS',
username: 'datadataBaseIMS',
password: 'ImSKrwData@123qweDevp',
timeoutInSeconds: 15,
);
if (isConnected) {
print('Database connected successfully!');
} else {
print('Failed to connect to the database.');
}
return isConnected;
} catch (e) {
print('Error connecting to database: $e');
return false;
}
}
// Method to execute a query
Future<String> executeQuery(String query) async {
try {
// First, check if the connection is open
if (await _isConnected()) {
print("Connection is open, executing query...");
String result = await _connection.getData(query);
return result; // Return the query result
} else {
print("Database is not connected. Cannot execute query.");
throw Exception('Database is not connected');
}
} catch (e) {
print('Error executing query: $e');
return 'Error executing query: $e';
}
}
// Helper method to check if the database is connected
Future<bool> _isConnected() async {
try {
bool isConnected = await _connection.isConnected;
return isConnected;
} catch (e) {
print('Error checking connection status: $e');
return false;
}
}
}
I am trying to connect my Flutter App to a SQL Server database with using dart plugin mssql_connection
, without using any API.
Can anyone tell me why I am getting this error?
Error connecting to database: MissingPluginException (no implementation found for method connect on channel mssql_connection)
import 'package:mssql_connection/mssql_connection.dart';
class DataHelper {
final MssqlConnection _connection = MssqlConnection.getInstance();
// Method to connect to the database
Future<bool> connectToDatabase() async {
try {
bool isConnected = await _connection.connect(
ip: 'iplink',
port: '1433',
databaseName: 'dataBaseIMS',
username: 'datadataBaseIMS',
password: 'ImSKrwData@123qweDevp',
timeoutInSeconds: 15,
);
if (isConnected) {
print('Database connected successfully!');
} else {
print('Failed to connect to the database.');
}
return isConnected;
} catch (e) {
print('Error connecting to database: $e');
return false;
}
}
// Method to execute a query
Future<String> executeQuery(String query) async {
try {
// First, check if the connection is open
if (await _isConnected()) {
print("Connection is open, executing query...");
String result = await _connection.getData(query);
return result; // Return the query result
} else {
print("Database is not connected. Cannot execute query.");
throw Exception('Database is not connected');
}
} catch (e) {
print('Error executing query: $e');
return 'Error executing query: $e';
}
}
// Helper method to check if the database is connected
Future<bool> _isConnected() async {
try {
bool isConnected = await _connection.isConnected;
return isConnected;
} catch (e) {
print('Error checking connection status: $e');
return false;
}
}
}
This plugin (mssql_connection) is only for Android, so I assume you're running your code on iOS, which causes the issue
Look at the image, you can see that this package only has android
folder, with native Kotlin language, so it doesn't have any Swift code to handle the native method connect
on iOS side
If you haven't known about platform-specific code, quickly explain: that some libs need to call to native side (Android & iOS native code) to access some native platform feature, which can't be done in Dart code, and mssql_connection is the one like that